Create a custom configuration section in asp.net (translation)

Source: Internet
Author: User
Tags add closing tag config net string tag name version root directory
Asp.net| Create |asp.net First, introduce

asp.net Web applications access simple "key/value" configuration data in a built-in way. In the Web.config file, you can create a section to store a simple "key/value" pair. For example, create a new ASP.net project and add the following markup as a child tag for the element in the Web.config file:









This section contains "key/value" pairs defined with two tags, and you can get their values from the ConfigurationSettings property built into the Page object. As a start, create a new Web Form named Customitems.aspx in your project, adding the following code to the form's Page_Load event:

Dim Akey as String

Response.Write ("

AppSettings
")

For each akey in ConfigurationSettings.AppSettings.Keys

Response.Output.WriteLine (Akey & "=" & _

ConfigurationSettings.AppSettings.Item (Akey))

Next

You can see the value of the tag by compiling the customitems.aspx Web form. The For loop retrieves all the tags in the section and displays the keys and their corresponding property values. This simple "key/value" mechanism is perfect for many general requirements, such as storing database connection strings throughout the application, but it is not robust enough for more complex data. Fortunately, Microsoft also created a mechanism to create custom configuration data, using the ASP.net framework to read one or more sections, rather than just reading a fixed list of tags through code in a particular application. section defines the name of the tag that the framework expects to discover in the rest of the Web.config file, while declaring the type and location of the class that handles its particular type of content.



When parsing a configuration file, ASP. NET engine creates a list of possible tokens by reading the element's markup, each of which contains a "name" and a "type" that declares the expected tag name and the corresponding configuration section handler in the rest of the file. Here's a little experiment to illustrate the whole process. In the project, before the tag at the end of the Web.comfig file, add a new tag as follows.











Saving the Web.config file and running the project will result in an "Unrecognized configuration section ' Customitems" error that occurred due to a section handler that did not have a declared tag. But if you browse through the Web.config file, you won't see any of the Tagged configuration section handler declarations, which poses a problem, where exactly are these configuration section handlers declared? (in reading this article, if you follow these steps at the same time, please delete the tag from the Web.config file before continuing.) )

In fact, each Web application has two profiles: the root machine.config file stored in the system folder and the Web.config file in your application's root directory. You can find the Machine.config file in the \microsoft.net\framework\\config folder under the operating system folder, which corresponds to the. NET framework that is installed and activated on the server. The settings for configuration in the Machine.config file apply to all applications on the server, unless they are reset by local settings. Browsing through the Machine.config file, you can see a tag that contains a set of tags that declare the configuration section handlers that you can see in the Web.config file for the default tags. To make this process easier to understand, you can go a step further and put tags in a tag, which holds a set of related section tags, respectively.

The reason I draw machine.config files is because there are two ways to add custom tags: you can resolve custom markup content with either default system configuration section handlers, or you can create your own configuration section handlers.



Using the System configuration section handler to resolve the custom tag

1. Create a new tag in the element, as follows:


Type= "System.configuration.namevaluesectionhandler,system, version=1.0.3300.0,culture=neutral,publickeytoken= b77a5c561934e089 "

/>

Author's note: The values of version and PublicKeyToken may be different from your. NET Framework version, and to find the correct values in the system simply copy from any existing element!

2. Test the newly created tag before the closing tag in the Web.config file, for example:







key= "Somekey" value= "somevalue"/>





3. Save the Web.config file and add the following highlighted code to the Page_Load event in the Customitems.aspx Web form:

Dim Akey as String

Response.Write ("

AppSettings
")

For each akey in ConfigurationSettings.AppSettings.Keys

Response.Output.WriteLine (Akey & "=" & _

ConfigurationSettings.AppSettings.Item (Akey))

Next

Response.Write ("

Customsystemitems
")

For each akey in CType (configurationsettings.getconfig _

("Customsystemitems"), _

System.Collections.Specialized.NameValueCollection). Keys

Response.Output.WriteLine (Akey & "=" & _

ConfigurationSettings.AppSettings.Item (Akey))

Next

4. Now compile and execute the Web form again. This time, you can see the Customsystemitem header information trailing a row of "Somekey=somevalue", which corresponds to a child element added to the element.

By modifying the Machine.config file, you can apply a defined custom tag to any Web application running on the server. But chances are you don't always want the tag handler to apply to all applications, and if so, you can add tags and tags to the web.config file instead of the Machine.config file. To test, first delete the tags defined in the Machine.config file and save them, then add a tag to the Web.config file followed by the start tag, and place the tag in it. For example:







Type= "System.configurati

[1] [2] [3] [4] Next page



Related Article

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.