Asp. NET contains multiple config files in a Web site (web. config file configsource

Source: Internet
Author: User
Tags connectionstrings

Each Web site will have a Web. config file. Modifying the Web. config file will cause IIS to restart, or a random carriage return will cause a reboot. Microsoft recommends that you do not save configuration content that needs to be modified in Web. config. Instead, it is placed in a single config. But how do you modify and read a config file that is stored separately? For example, you can specify that appsetting in Web. config be placed separately in a config.config file. specified by ConfigSource.

First, the original Web. config file:


<?xml version= "1.0" encoding= "Utf-8"?><configuration>  <appSettings>    <add key= " Cachetimeinfo "value=" "/>    <add key=" Cachetimenews "value=" ten "/> <add    key=" Cachetimeproduct " Value= "/>    <add key=" Cachetimetrade "value=" 5 "/>    <add key=" SiteName "value=" China Fork Net "/>    <add key= "SiteDomain" value= "chinaxx.com"/>  </appSettings>  <connectionStrings/>  <system.web>    <compilation debug= "false" >    </compilation>    <authentication mode = "Windows"/>  </system.web></configuration>
Split into two config files below


1) First config file: Web. config


<?xml version= "1.0" encoding= "Utf-8"?><configuration> <appsettings  configsource= "App-1.config" /><connectionstrings/>  <system.web>    <compilation debug= "false" >    </ compilation>    <authentication mode= "Windows"/>  </system.web></configuration>


2) Second config file: app-1.config


<?xml version= "1.0" encoding= "Utf-8"? ><appsettings><add key= "Cachetimeinfo" value= "+"/><add key= "Cachetimenews" value= "ten"/><add key= "cachetimeproduct" value= ""/><add key= "CacheTimeTrade" value = "5"/><add key= "SiteName" value= "China Fork Net"/><add key= "SiteDomain" value= "chinaxx.com"/></ Appsettings>

So how to read it?

CS0122: "System.configuration.configurationelement.this[system.configuration.configurationproperty]" not accessible, Because it is protected by a level limit

The correct reading method is:

Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration ( Request.applicationpath); string SiteName = config. appsettings.settings["SiteName"]. Value;



To modify the config value:

Configuration cfg = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration (request.applicationpath ); Appsettingssection appsetting = cfg. appsettings;appsetting.settings["SiteName"]. Value = "changed by Application"; Modify the value of SiteName in Config.config Cfg.save ();



Summary: After splitting, modifying config.config does not cause IIS to restart.

Description: My app-1.config file and webconfig in the same level directory, in addition, I am called app-1.config here, you can change, such as the name of app.

Pay attention to the wording of app-1.config.
Once you add configsource to the appsettings node in Web. config, you can no longer add a node. That means you can't put a part of the <add> in the Webconfig, and then the Config.config. When you put part <add> modify, the node that is accessed must exist or it is reported that the object reference is not set to an instance of the object.

Asp. NET contains multiple config files in a Web site (web. config file configsource

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.