开发者

Is it possible to create a location element in web.config programmatically (using ConfigurationLocation for example)

开发者 https://www.devze.com 2023-03-13 03:23 出处:网络
I can access and modify the contents of a <location> tag in web.config, starting from: Configuration config = WebConfigurationManager.OpenWebConfiguration(virtualFolderPath);

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

0

精彩评论

暂无评论...
验证码 换一张
取 消