When I have a specific setting in web.config, say the session state configurat开发者_开发百科ion, and I want to programmatically determine the configuration at Application_Start, how would I go about this. I am trying to have a slightly different configuration per environment.
- I do not want to overwrite the real web.config (prevent restarts)
- I know the solution presented in Use XML includes or config references in app.config to include other config files' settings, but I don't want the whole system.web section in a separate file, and system.web does not implement a file attribute like appSettings and connectionStrings do.
Any ideas?
I don't think that you can alter settings from the web.config at runtime without it reloading the application. As the article in @ChristopheD's answer shows, you can read and write web.config settings at runtime, but saving them restarts the application when it writes out the changes. I'm pretty sure that once your app gets to the point where you can do this ASP.NET has already read them in and changing them at runtime without saving them will have no effect.
FWIW, I'd recommend against this type of environment-specific manipulation at runtime. A better approach is to alter the web.config per environment at build or deployment time. MSDeploy has a way to do this, and that functionality is coming to VS2010.
The following link should be helpful...
PS: It's the first thing you find when you type 'alter web.config programmatically' into google.
精彩评论