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.