views:

416

answers:

2

When I have a specific setting in web.config, say the session state configuration, 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.

Any ideas?

+1  A: 

The following link should be helpful...

PS: It's the first thing you find when you type 'alter web.config programmatically' into google.

ChristopheD
+1 for you, just way of giving answer....Grate....
Muhammad Akhtar
That sample shows how to change web.config. However, as stated in the question, I don't want to overwrite the file to prevent restarts. ASP.NET will not see any of the changes you make this way until you save the file.
Teun D
+1  A: 

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.

John Clayton
Correct, for the config changes to take effect one must restart AppDomain. In case of ASP.NET that means restart of your application with all the consequences (loosing cache content, all user sessions for in-memory session, etc).
UserControl
Thanks; clear answer. Not what I hoped for, but I will have a better look at MSDeploy.
Teun D