Configsections custom configuration section learning instance
. Aspx. CS
Public partial class _ default: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
// Lcscopyright configuration section
LCS. configration. Copyright data;
// Data = system. configuration. configurationsettings. getconfig ("lcscopyright") as LCS. configration. Copyright; // expired
Data = system. configuration. configurationmanager. getsection ("lcscopyright") as LCS. configration. Copyright; // get the data in the configuration section.
Response. Write (data. appname );
Response. Write (data. appver );
// Output
// LCS configration test 1.0 bate
Response. Write ("<HR/> ");
// Lcsappconfig configuration section
// Load the configuration statement in application_start of Global. asax.
System. configuration. configurationsettings. getconfig ("lcsappconfig ");
Response. Write (LCS. configration. appconfig. connectionstring );
Response. Write (LCS. configration. appconfig. usercount );
// Output
// This Is A connectionstring199
}
}
Web. config
<? XML version = "1.0" encoding = "UTF-8"?>
<Configuration>
<Configsections>
<Section name = "lcscopyright" type = "LCS. configration. Settings"/> <! -- If a generated DLL name exists, the DLL name must be added later. Program Set Name -->
<Section name = "lcsappconfig" type = "LCS. configration. appconfig"/> <! -- Here, the experiment is directly put in app_code, and no assembly name needs to be written. -->
</Configsections>
<Lcscopyright>
<Appname> LCS configration test </appname>
<Appver> 1.0 bate </appver>
<Appauthor> lcskey </appauthor>
</Lcscopyright>
<Lcsappconfig>
<Add key = "connectionstring" value = "This Is A connectionstring"/>
<Add key = "usercount" value = "199"/>
</Lcsappconfig>
</Configuration>
. CS
Using system;
Using system. Collections. Generic;
Using system. Web;
Using system. xml;
Namespace LCS. configration
{
Public class settings: system. configuration. iconfigurationsectionhandler
{
/// <Summary>
/// Create method for implementing this interface
/// The configurationsettings. getconfig ("") method automatically calls this method to assign values to the corresponding object attributes.
/// </Summary>
/// <Param name = "parent"> </param>
/// <Param name = "input"> </param>
/// <Param name = "Node"> </param>
/// <Returns> </returns>
Public object create (Object parent, object input, xmlnode node)
{
Copyright DATA = new Copyright ();
Foreach (xmlnode Xn in node. childnodes)
{
Switch (Xn. Name. tolower ())
{
Case ("appname "):
Data. appname = xn. innertext;
Break;
Case ("appver "):
Data. appver = xn. innertext;
Break;
Case ("appauthor "):
Data. appauthor = xn. innertext;
Break;
}
}
Return data;
}
}
/// <Summary>
/// The entity corresponding to the copyright configuration section Information read from the configuration section
/// </Summary>
Public class Copyright
{
Public String appname;
Public String appver;
Public String appauthor;
}
/// <Summary>
/// All configurations are mapped to the corresponding static member variables.
/// And is written as a read-only attribute
/// Such a program
/// Appconfig. connectionstring
/// You can access the project in the configuration file.
/// Call before reading
/// System. configuration. configurationsettings. getconfig ("lcsappconfig ");
/// Call the appconfig that implements the iconfigurationsectionhandler interface to read the configuration
/// </Summary>
Public class appconfig: system. configuration. iconfigurationsectionhandler
{
Static string m_connectionstring = string. empty;
Static int32 m_usercount = 0;
Public static string connectionstring
{
Get
{
Return m_connectionstring;
}
}
Public static int32 usercount
{
Get
{
Return m_usercount;
}
}
Static string readsetting (system. Collections. Specialized. namevaluecollection NVC, string key, string defaultvalue)
{
String thevalue = NVC [Key];
If (thevalue = string. Empty)
Return defaultvalue;
Return thevalue;
}
Public object create (Object parent, object configcontext, xmlnode Section)
{
System. Collections. Specialized. namevaluecollection settings;
try
{< br> system. configuration. namevaluesectionhandler basehandler = new system. configuration. namevaluesectionhandler ();
Settings = (system. collections. specialized. namevaluecollection) basehandler. create (parent, configcontext, Section);
}< br> catch
{< br> Settings = NULL;
}
If (settings! = NULL)
{
M_connectionstring = appconfig. readsetting (settings, "connectionstring", String. Empty );
M_usercount = convert. toint32 (appconfig. readsetting (settings, "usercount", "0 "));
}
Return settings;
}
}
}
Separation configuration section
Web. config
<? XML version = "1.0" encoding = "UTF-8"?>
LCS configration test
1.0 bate
lcskey
-->
<! -- <Lcsappconfig>
<Add key = "connectionstring" value = "This Is A connectionstring"/>
<Add key = "usercount" value = "199"/>
</Lcsappconfig> -->
<Etetaskconfigsource = "app. config"/>
<Connectionstrings configsource = "conn. config"/>
<! -- Remove custom configuration sections -->
<Lcscopyright configsource = "lcscopyright. config"/>
<Lcsappconfig configsource = "lcsappconfig. config"/>
</Configuration>
App. config
<Deleetask>
<Add key = "A" value = "changed by application RunTime"/>
</Appsettings>
Conn. config
<Connectionstrings>
<Add name = "C" connectionstring = "dbconnention... 123"/>
</Connectionstrings>
Lcscopyright. config
<? XML version = "1.0" encoding = "UTF-8"?>
<Lcscopyright>
<Appname> LCS configration test </appname>
<Appver> 1.0 bate </appver>
<Appauthor> lcskey </appauthor>
</Lcscopyright>
Lcsappconfig. config
<? XML version = "1.0" encoding = "UTF-8"?>
<Lcsappconfig>
<Add key = "connectionstring" value = "This Is A connectionstring"/>
<Add key = "usercount" value = "199"/>
</Lcsappconfig>
Modify web. config. Add the preceding file in the same directory.
Run the program and check whether all data can be read in the same way.
. AspxStill original, not dynamicCode
Public partial class _ default: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
// Lcscopyright configuration section
LCS. configration. Copyright DATA = new LCS. configration. Copyright ();
// Data = system. configuration. configurationsettings. getconfig ("lcscopyright") as LCS. configration. Copyright; // expired
Data = system. configuration. configurationmanager. getsection ("lcscopyright") as LCS. configration. Copyright; // get the data in the configuration section.
Response. Write (data. appname );
Response. Write (data. appver );
// Output
// LCS configration test 1.0 bate
Response. Write ("<HR/> ");
// Lcsappconfig configuration section
// Load the configuration statement in application_start of Global. asax.
System. configuration. configurationsettings. getconfig ("lcsappconfig ");
Response. Write (LCS. configration. appconfig. connectionstring );
Response. Write (LCS. configration. appconfig. usercount );
// Output
// This Is A connectionstring199
// Break down web. config
Response. Write ("<HR/> ");
// appsettings configuration section
response. write (system. configuration. configurationmanager. appsettings ["A"]);
response. write ("
");
// connectionstrings configuration section
response. write (system. configuration. configurationmanager. connectionstrings ["C"]);
// Modify the appsettings configuration section without restarting the application
// If you need to restart, you can set the restartonexternalchanges attribute of the configuration section.
System. configuration. Configuration CFG = system. Web. configuration. webconfigurationmanager. openwebconfiguration (request. applicationpath );
System. configuration. Maid;
Deleetting. settings ["A"]. value = "changed by application RunTime ";
Cfg. Save ();
}
}
Refer toArticleAnd msdn. Learn to sell now.
In the past, only app and conn were separated. the user-defined only thought that app and conn could be separated, but they never tried it. I tried it today, but it's not bad.
Source code of instances that can run directly