I can access and modify the contents of a <location> tag in web.config, starting from:
Configuration config = WebConfigurationManager.OpenWebConfiguration(virtualFolderPath);
ConfigurationLocationCollection locations = config.Locations;
then for each location:
C开发者_开发问答onfiguration thisConfig = location.OpenConfiguration();
However, I cannot see any way to add a new <location>, is that possible?
The web.config is a legal XML document. So, other considerations aside, there is nothing to stop you from loading it and using either XMLDocumment / XPath or XDocument methods to modify and save the file.
A ConfigurationLocationCollection
is derived from a System.Collections. ReadOnlyCollectionBase
which is read-only. So using the configuration API's as they are just now this is not possible.
I would suggest to you to not do this programmatically, because whenever you are trying to add/edit
values in the web.config, your application restarts and all users session will be terminated.
check this article for Details Common reasons for the session loss issue in ASP.NET applications
精彩评论