Configuration File web. config (2)

Source: Internet
Author: User
Tags configuration settings
From msdn

You can use your own xml configuration elements to expand the standard ASP. NET configuration set. To complete this operation, you must create your own configuration section handler.

The handler must be Class. NET Framework class. Section handler explains and processes XML in a specific part of the Web. config file
Configure the settings defined in the element, and return the appropriate configuration object according to the configuration settings. The configuration object returned by the handler class can be any data structure; it is not limited to any base configuration class or configuration format. ASP. NET
Use this configuration object to read and write custom configuration elements.

Create custom configuration section
To create a custom configuration section, we recommend that you create an assembly that specifically processes the custom configuration section and create a processing class for each custom configuration set. We usually place the custom configuration section in the sectiongroup of configsecions. Note that the configuration section handler must be declared before configuration processing.

Set Element and

Add element
In the Web. config file Element, as shown in the following code example. It is this statement that associates the custom node handler with the node name.

SetSectionElement Nested inSectiongroup
Is optional, but it is recommended to do so to better organize the configuration data.

You can add the section handler declaration in another configuration file, which does not need to be a configuration file that adds custom configuration elements, you only need to declare that the configuration file of the section handler is in a high position in the configuration file hierarchy.

That is to say:

SectionElementTypeThe property must match the Assembly List; otherwise, a configuration error occurs. The Assembly file must be associated with the Web. config
The file is located in the same ASP. NET application directory. 

<Configuration>
<! -- Configuration section-handler declaration area. -->
<Configsections>
<Sectiongroup name = "mycustomgroup">
<Section
Name = "mycustomsection"
Type = "myconfigsectionhandler. myhandler, mycustomconfigurationhandler, version = 1.0.0.0, culture = neutral, publickeytoken = NULL"
Allowlocation = "true"
Allowdefinition = "everywhere"
/>
</Sectiongroup>
<! -- Other <section> and <sectiongroup> elements. -->
</Configsections>

<! -- Configuration section Settings area. -->

<Mycustomgroup>
<Mycustomsection myattrib1 = "Clowns">
<Mychildsection
Mychildattrib1 = "Zippy"
Mychildattrib2 = "Michael zawondy"/>
</Mycustomsection>
</Mycustomgroup>
<! -- Note that the mychildsection element in the mycustomgroup element does not have the configuration section handler declaration. This is because the mycustomsection processing program
Process all child elements in the mychildsection Setting section of its subnode. -->
</Configuration>

Create a custom configuration section Handler
The structure of the custom configuration section handler is as follows:
Using system;
Using system. collections;
Using system. text;
Using system. configuration;
Using system. xml;

Namespace myconfigsectionhandler
{
Public class myhandler: configurationsection
{
Public myhandler ()
{
}

// Add declarations for child elements and attributes like this:
// [Configurationproperty ("<propertyname>", <named parameters>)]
// Public <type> <propertyname>
//{
// Get {return (<type>) This ["<propertyname>"];}
// Set {This ["<propertyname>"] = value ;}
//}
}
}
Add your own code to perform the required configuration. As follows:
Using system;
Using system. collections;
Using system. text;
Using system. configuration;
Using system. xml;

Namespace myconfigsectionhandler
{
Public class myhandler: configurationsection
{
Public myhandler ()
{
}

Public myhandler (string attribval)
{
Myattrib1 = attribval;
}

// Attribute Declaration for each node
[Configurationproperty ("myattrib1", defaultvalue = "Clowns", isrequired = true)]
[Stringvalidator (invalidcharacters = "~! @ # $ % ^ & * () [] {}/; '\ "| \", Minlength = 1, maxlength = 60)]
Public String myattrib1
{
Get
{Return (string) This ["myattrib1"];}
Set
{This ["myattrib1"] = value ;}
}

[Configurationproperty ("mychildsection")]
Public mychildconfigelement mychildsection
{
Get
{Return (mychildconfigelement) This ["mychildsection"];}
Set
{This ["mychildsection"] = value ;}
}
}

Public class mychildconfigelement: configurationelement
{
Public mychildconfigelement ()
{
}

Public mychildconfigelement (string A1, string A2)
{
Mychildattribute1 = A1;
Mychildattribute2 = a2;
}

[Configurationproperty ("mychildattrib1", defaultvalue = "Zippy", isrequired = true)]
[Stringvalidator (invalidcharacters = "~! @ # $ % ^ & * () [] {}/; '\ "| \", Minlength = 1, maxlength = 60)]
Public String mychildattribute1
{
Get
{Return (string) This ["mychildattrib1"];}
Set
{This ["mychildattrib1"] = value ;}
}

[Configurationproperty ("mychildattrib2", defaultvalue = "Michael zawondy", isrequired = true)]
[Stringvalidator (invalidcharacters = "~! @ # $ % ^ & * () [] {}/; '\ "| \", Minlength = 1, maxlength = 60)]
Public String mychildattribute2
{
Get
{Return (string) This ["mychildattrib2"];}
Set
{This ["mychildattrib2"] = value ;}
}
}
}

Access custom configuration data programmatically

Obtain an instance of the custom configuration object and use Method or To fill the instance.

The following ASPX page uses the previous example to enumerate attributes and child elements of a custom configuration section.

<% @ Page Language = "C #" %>

<SCRIPT runat = "server">
Protected void button#click (Object sender, eventargs E)
{
Myconfigsectionhandler. myhandler Config =
(Myconfigsectionhandler. myhandler) system. configuration. configurationmanager. getsection (
"Mycustomgroup/mycustomsection ");

Stringbuilder sb = new stringbuilder ();

SB. append ("<H2> attributes in the mycustomsection element: </H2> ");
SB. appendformat ("myattrib1 = {0} <br/>", config. myattrib1.tostring ());
SB. append ("<H2> attributes in the mychildsection element: </H2> ");
SB. appendformat ("mychildattrib1 = {0} <br/>", config. mychildsection. mychildattribute1.tostring ());
SB. appendformat ("mychildattrib2 = {0} <br/>", config. mychildsection. mychildattribute2.tostring ());

Label1.text = sb. tostring ();
Label1.visible = true;
}
</SCRIPT>

<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> untitled page </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>

<H1> enumerate mycustomsection <Asp: Label id = "label1" runat = "server"
TEXT = ""/>
<Br/>
<Asp: button id = "button1" runat = "server"
TEXT = "Get custom config Info"
Onclick = "button#click"/>

</Div>
</Form>
</Body>
</Html>

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.