Saturday, November 3, 2018

asp.net - 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?


Answer



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.


No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...