Create a custom configuration section in ASP. NET)

Source: Internet
Author: User
I. INTRODUCTION
ASP. NET Web applications use a built-in method to access simple "key/value" configuration data. In the Web. config file, you can create a section to store simple "key/value" pairs. For example, to create an ASP. NET project, add the following tag to the Web. config file as the sub-tag of the element:


This section contains "key/value" pairs defined with two tags. You can obtain their values through the built-in ConfigurationSettings attribute of the Page object. At the beginning, create a new Web form named customItems. aspx in your project and add the following code to the Page_Load event of the form:
Dim aKey As String
Response. Write ("
AppSettings
")
For Each aKey In ConfigurationSettings. Receivettings. Keys
Response. Output. WriteLine (aKey & "= "&_
ConfigurationSettings. Deleettings. Item (aKey ))
Next
Compile and run customItems. aspx Web form to view the tag value. The For loop retrieves all the tags in the section and displays the keys and their corresponding attribute values. This simple "key/value" mechanism is perfect for many general requirements, such as storing database connection strings throughout the application, however, it is not robust enough for more complex data. Fortunately, Microsoft has also established a mechanism for creating custom configuration data, using ASP. the. NET Framework reads one or more sections, instead of reading a fixed tag list only through the code in a specific application. This section defines the tag names that the framework expects to discover in the rest of the Web. config file, and declares the types and locations of classes that process specific types of content.
ASP. the NET engine creates a possible tag list by reading the element's tag. Each tag contains a "name" and a "type ", declares the expected tag name and corresponding configuration section handler in the remaining content of the file. The following uses a small experiment to demonstrate the entire working process. Add a new mark as follows to the front of the mark at the end of the Web. comfig file in the project.


Save Web. when the config file is run and the project is run, an "unrecognized configuration section" customItems "error is returned. This error is caused by the section handler without the declaration mark. However, if you browse the entire Web. in the config file, you won't see any marked configuration section handler declaration, which brings up a problem where the configuration section handler declares it? (When reading this article, if you have followed these steps at the same time, delete the mark from the Web. config file before proceeding .)
In fact, each Web application has two configuration files: The Root machine. config file saved in the system folder and the Web. config file saved in the root directory of your application. You can find the machine. config file in the Microsoft. NETFrameworkCONFIG folder under the operating system folder, which corresponds to the. NET Framework installed and activated on the server. The configuration settings in the machine. config file apply to all applications on the server, unless they are reset by local settings. Browse the entire machine. config file and you will see a set of tags that declare the default configuration section handlers you can see in the Web. config file. To make this process easier to understand, you can further put Tag groups in tags, which store a group of Associated section tags.

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.