简体   繁体   中英

How to programmatically override web.config settings

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM