Extension of the web. config configuration file structure

Source: Internet
Author: User
ArticleDirectory
    • (1) create a section class
    • (2) Registration Section class
    • (3) Compile the code to get the custom section information on the Web page.
    • Expand custom configuration again

We know that you can use the <deleettings> element to save the application.ProgramThe custom information used. However, this element has two obvious restrictions.

1. You cannot save structured information, such as a list or a set of related settings.

2. Various data types cannot be processed flexibly. This element setting can only use a single string.

 

Fortunately, ASP. NET uses a modular highly scalable configuration model that allows you to use custom sections to expand the structure of the web. config and machine. config configuration files.

Perform the following three steps:

1. determine the information to be saved in the configuration file and how to organize them into elements and features.

2. For each new element, create a C # class to encapsulate its information.

3. to register a new section in the configuration file, you must use the <configsections> element to identify each new element and map it to the relevant class.

 

The best way to understand the above theoretical steps is practice. The following example shows how to create and register new elements in Web. config:

(1) create a section class

If you want to save a set of related settings, they will tell the application how to contact remote objects. These settings can represent the port number, server location, URL, user authentication information, and so on. In the previous method, you can configure it separately in <deleettings>. However, nothing indicates that the information is related to each other. This not only makes the setting difficult to read and understand, but may also update one of the settings and forget to update other related settings.

A better solution isGet rid of the structure restrictions of the <deleetask> section and wrap the information in a separate XML element.

<Orderservice Available= "True" Polltimeout= "00:01:00" Location= "TCP: // ordercomputer: 8010/orderservice" />

To use this structure, you also need to define a matching class inherited from system. configuration. configurationsection.This class can be placed in an independent DLL component orSource codePut it in the app_code folder, so that it will be automatically compiled as part of the current web program.

Note: All types obtained by the getsection () method are inherited from the system. configuration. configurationsection class!

The following orderservice class plays this role. It represents a <orderservice> element and provides access to three features through a strongly typed attribute:

 
Public ClassOrderservice: configurationsection

 
{

 
[Configurationproperty ("Available", Isrequired =False, Defaultvalue =True)]

 
Public BoolAvailable

 
{

Get {Return(Bool)Base["Available"];}

 
Set {Base["Available"] =Value;}

 
}

 
 

 
[Configurationproperty ("Polltimeout", Isrequired =True)]

 
PublicTimespan polltimeout

 
{

Get {Return(Timespan)Base["Polltimeout"];}

 
Set {Base["Polltimeout"] =Value;}

 
}

 
 

 
[Configurationproperty ("Location", Isrequired =True)]

 
Public StringLocation

 
{

Get {Return(String)Base["Location"];}

 
Set {Base["Location"] =Value;}

 
}

 
}

You can see that each property uses the configurationproperty Property to map to the corresponding property name. This part is very important because it defines the structure of the custom section. If a feature is added but does not match the configurationproperty feature in the Custom section, ASP. NET will throw an exception when reading the Web. config file..

The isrequired attribute of the configurationproperty feature allows you to determine which information is required. If the default value is not provided, you can decide what default value to use..

 

(2) Registration Section class

After the section is created, the encoding is completed. However, you must register the section class in the web. config file so that ASP. NET can recognize custom settings. Otherwise, an error occurs when running the application. An unrecognized section is displayed in the web. config file.

As long. you can add a <section> element to the <configsections> section of the config file to register a custom section. However, you must specify the name of the Section (through the name feature ), specifies the name of the corresponding class (through the Type feature ).

 
<Configuration>

 
<Configsections>

 
<Section Name= "Orderservice" Type= "Orderservice"/>

 
</Configsections>

 
 

 
<System. Web>

<Compilation Debug= "True" Targetframework= "4.0"/>

 
<Trace Enabled= "True" Requestlimit= "10" Pageoutput= "False" Tracemode= "Sortbytime" Localonly= "True" />

 
</System. Web>

 
 

 
<Orderservice Available= "True" Polltimeout= "00:01:00" Location= "TCP: // ordercomputer: 8010/orderservice" />

 
</Configuration>

 

(3) Compile Code Get custom section information on the Web Page
 
Protected VoidPage_load (ObjectSender, eventargs E)

 
{

 
ObjectOBJ = configurationmanager. getsection ("Orderservice");

 
Orderservice OS = OBJAsOrderservice;

 
Label1.text + ="Retrieved service information... <br/>"

+"<B> location: </B>"+ OS. Location

 
+"<Br/> <B> available: </B>"+ OS. Available

 
+"<Br/> <B> Timeout: </B>"+ OS. polltimeout +"<Br/>";

 
}

Page display:

 

 

Expand custom configuration again

The custom configuration section can be more complex, for example, creating a section with nested child elements. See the following example:

<Orderservice Available= "True" Polltimeout= "00:01:00">

 
<Location Computer= "Ordercomputer" Port= "8010" Endpoint= "Orderservice" />

 
</Orderservice>

If such a structure is used, you only need to create a class inherited from configurationelement to represent each nested element..

 
/// <Summary>

 
/// Nested child elements must inherit from the configurationelement class

 
/// </Summary>

 
Public ClassLocation: configurationelement

 
{

 
[Configurationproperty ("Computer", Isrequired =True)]

Public StringComputer

 
{

 
Get {Return(String)Base["Computer"];}

 
Set {Base["Computer"] =Value;}

 
}

 
 

 
[Configurationproperty ("Port", Isrequired =True)]

 
Public IntPort

 
{

 
Get {Return(Int)Base["Port"];}

 
Set {Base["Port"] =Value;}

 
}

 
 

 
[Configurationproperty ("Endpoint", Isrequired =True)]

 
Public StringEndpoint

 
{

 
Get {Return(String)Base["Endpoint"];}

 
Set {Base["Endpoint"] =Value;}

 
}

 
}

The original orderservice class is changed to the following:

Public ClassOrderservice: configurationsection

 
{

 
[Configurationproperty ("Available", Isrequired =False, Defaultvalue =True)]

 
Public BoolAvailable

 
{

 
Get {Return(Bool)Base["Available"];}

Set {Base["Available"] =Value;}

 
}

 
 

 
[Configurationproperty ("Polltimeout", Isrequired =True)]

 
PublicTimespan polltimeout

 
{

 
Get {Return(Timespan)Base["Polltimeout"];}

Set {Base["Polltimeout"] =Value;}

 
}

 
 

 
[Configurationproperty ("Location", Isrequired =True)]

 
PublicLocation// The attribute here is of the child element type

 
{

 
Get {Return(Location)Base["Location"];}

Set {Base["Location"] =Value;}

 
}

 
}

Here we call:

 
Protected VoidPage_load (ObjectSender, eventargs E)

 
{

 
ObjectOBJ = configurationmanager. getsection ("Orderservice");

 
Orderservice OS = OBJAsOrderservice;

Label1.text + ="Retrieved service information... <br/>"

 
+"<Br/> <B> available: </B>"+ OS. Available

 
+"<Br/> <B> Timeout: </B>"+ OS. polltimeout +"<Br/>"

 
+"<B> server: </B>"+ OS. Location. Computer +"<Br/>"

 
+"<B> port: </B>"+ OS. Location. Port +"<Br/>"

 
+"<B> endpoint: </B>"+ OS. Location. endpoint +"<Br/>";

 
}

Final effect:

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.