Overview of ASP. NET configuration files (III)

Source: Internet
Author: User

This article describes how to read and write the Web. config file. Not everyone can perform read and write operations on the Web. config file. You must have certain permissions.
In fact, in the web. many configuration nodes in config correspond to a class. We can use the corresponding class to access the corresponding configuration by using its strong-type API, instead of using the web. config is read as XML.
You can access the configuration file in two ways. First, you can access the configuration file using a strong API, which requires the configurationmanager class. As follows:

Using System. Web. configuration;
Using System. configuration;
 
Protected   Void Page_load ( Object Sender, eventargs E)
{
Sessionstatesection STS
= (Sessionstatesection) configurationmanager. getsection ( " System. Web/sessionstate " );
Response. Write (
" The session state mode is: "   + STS. mode. tostring () +   " <Br/> " );
}

 

 

As you can see, we are actually accessing the sessionstate node.
There is a static getsection method in configurationmanager, which is used to input the XPath to access the node, as shown in the above system. web/sessionstate, and then convert the result to the corresponding strong type, we can access the node attributes and other information. In the system. Web. configuration space, we can find the strong type of the corresponding node.

One thing to note is that the configurationmanager can only process web. config in the current website, that is, it cannot process the configuration files in other websites. For example, if our website is demoaCodeThe Web. config in demoa is accessed, and the Web. config in demob cannot be accessed.

But we can also use the second method, that is, webconfigurationmanager. The Code is as follows:

 

Using System. Web. configuration;
Using System. configuration;
 
Protected   Void Page_load ( Object Sender, eventargs E)
{
Membershipsection MS
= (Membershipsection) webconfigurationmanager. getsection ( " System. Web/Membership " , " ~ /Web. config " );
Response. Write (
" The default provider as set in config is: "   +
Ms. defaultprovider
+   " <Br/> " );}
}

 

 

In the code above, note that the second parameter of getsection declares web. config path. The above code accesses the configuration file of the current website. We also visit the configuration file of other websites.

The above describes how to read a configuration file. The following describes how to write the configuration file.
When writing a configuration file, we need to open the configuration file, not just to return a reference to the configuration file. That is to say, we cannot use configurationmanager for write operations, because it only returns a reference to system. configuration. configuration. Therefore, we should use webconfigurationmanager to write the configuration file.
After opening the configuration file programmatically and setting some properties, we can use configuration. Save () to save the changes. For example:

 

Using System. Web. configuration;
 
Protected   Void Page_load ( Object Sender, eventargs E)
{
Configuration config
= Webconfigurationmanager. openwebconfiguration ( " ~ " );
Membershipsection MS
= (Membershipsection) config. getsection ( " System. Web/Membership " );
Ms. defaultprovider
=   " Someotherprovider " ;
Config. Save ();
}

 

Note: The openwebconfiguration method has many reloads ,"~ "Indicates that the configuration file of the current website is opened. Another point is that the configuration file system does not synchronize and lock multiple threads. Therefore, it is best to pay attention to this when writing a configuration file.

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.