Add a custom configuration area for the Asp.net Program

Source: Internet
Author: User
We usually save configuration information such as SQL connection string in the appsettings section of Web. config to facilitate Program And can be obtained in the program using the following methods:
String sqlstr = system. configuration. configurationsettings. etettings ["connectionstring"];

For custom configurations with complex structures, You can implement this mechanism by implementing the iconfigurationsectionhandler interface. First, create the mysettings class, which only contains some custom configuration definitions that I need:

Using System;

Namespace Myconfig
{
Public   Class Mysettings
{
Public   String Somesetting; // Customize configurations of the string type

Public Mysettings ()
{
}
}
}

The next key step is to create a myconfighandler to process the configured mysettings. You need to implement the iconfigurationsectionhandler interface. The iconfigurationsectionhandler has only one method:

Object create (
Object Parent,
Object Configcontext,
Xmlnode Section
);

Because the Web. config file is a standard XML file, you can easily read the value of xmlnode:

Using System;
Using System. configuration;
Using System. xml;

Namespace Myconfig
{
Public   Class Myconfighandler: iconfigurationsectionhandler
{
Public Myconfighandler ()
{
}

Public   Object Create ( Object Parent, Object Input, xmlnode node)
{
Mysettings myset =   New Mysettings ();
Foreach (Xmlnode n In Node. childnodes)
{
Switch (N. Name)
{
Case ( " Somesetting " ): // Read somesetting value from web. config
Myset. somesetting = N. innertext;
Break ;
Default :
Break ;
}
}

Return Myset;

}

}
}

So far, all the custom configuration classes and handler have been created, and you only need to tell Web. config to use myconfighandler to process mysettings. You need to add the following content in Web. config:

< Configsections >
< Section Name = "Mysettings" Type = "Myconfig. myconfighandler, myconfig" > </ Section >
</ Configsections >

< Mysettings >
< Somesetting > This is a customer configuration setting. </ Somesetting >
</ Mysettings >

<Configsecions> tells Web. config to call myconfighandler to process mysettings. <mysettings> stores the custom configuration content. For example, execute the following command in a web page:Code:

Private   Void Page_load ( Object Sender, system. eventargs E)
{
// Put user code to initialize the page here
Mysettings myset;
Myset = System. configuration. configurationsettings. getconfig ( " Mysettings " ) As Mysettings;

Response. Write (myset. somesetting );
}

 

The result is displayed on the client. This is a customer configuration setting. In fact, another simpler method is to use namevaluefilesectionhandler, however, when adding configuration information, you need to use <Add name = "" value = ""> </Add> to add a key value, as in the deleteworkflow, it is of little significance for custom configurations. For details, refer to the relatedArticle.

Justin always pushes me more technical posts; otherwise, I am sorry. He recommended me here. If I have no time, I will write more articles about custom httphandlers and httpmodules, hoho, It's really uncomfortable to be pushed.

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.