A recent project needs to write a lot of configuration items and find that writing in a single Web. config can be messy and difficult to find
So the search was settled, and it was recorded.
First, Webconfig provides a way to introduce other Config
<connectionstrings configsource= "Configs\database.config"/>
This is the configuration of the connection string you can have in database. Config contains a lot of link strings in case you call
Database The contents of config are as follows:
<? XML version= "1.0" encoding= "Utf-8" ?> < connectionStrings > < name= "Sdbcontext" connectionString= "Server=.;i Nitial catalog=self; User Id=sa; Password=password " providerName=" System.Data.SqlClient "/></ connectionStrings>
<appsettings configsource= "Configs\system.config"/>
This is the way the key-value pairs are stored in the following code:
<?XML version= "1.0" encoding= "Utf-8"?><appSettings> <!--================== 1: Development of system-related configuration ================== - <!--Login Provider mode: Session, Cookie - <AddKey= "Loginprovider"value= "Cookie" /> <!--Enable System log - <AddKey= "Islog"value= "true" /> <!--Database over time - <AddKey= "CommandTimeout"value= " the" /> <!--Enable IP Filtering - <AddKey= "Isipfilter"value= "false" /> <!--================== 2: System software parameter configuration ================== - <!--Contact Us - <AddKey= "Contact"value= "TE software (Mobility)" /> <!--Software name - <AddKey= "Softname"value= "Sub self" /> <!--software version - <AddKey= "Version"value= "1.0" /> <!--set to apply path - <AddKey= "AppName"value="" /> <!--set to apply path - <AddKey= "Sqlgetbomlist"value="" /></appSettings>
Above two do not need special configuration, put in the configuration inside the configsections below so that you can
Second, the following introduction of the Custom configuration section
<configsections> < Sectionname= "Users"type= "System.Configuration.NameValueSectionHandler"/> </configsections> <UsersConfigSource= "Users.config"></Users>
Note that one of the configsections is a statement of how this is organized
The contents of Users.config are as follows:
<Users> <AddKey= "Beijing"value= "123"></Add> <AddKey= "Tianjin"value= "123"></Add></Users>
How to get the configuration:
NameValueCollection users = System.Configuration.ConfigurationManager.GetSection ("users") as NameValueCollection; Response.Write (Users. keys[0]+ "</br>" +users. KEYS[1]);
Three, Complex type:
The declaration of a complex type is different.
<configsections> < Sectionname= "Roles"type= "EBuy.Chapter3.NTier.WebUI.RolesConfig, EBuy.Chapter3.NTier.WebUI"/> </configsections><RolesConfigSource= "Roles.config"> </Roles>
The contents are as follows
<Roles> <Addusername= "Beijing"Password= "123"></Add> <Addusername= "Tianjin"Password= "123"></Add></Roles>
How to obtain:
Using system;using system.collections.generic;using system.linq;using system.web;namespace ebuy.chapter3.ntier.webui{public class RolesConfig:System.Configuration.IConfigurationSectionHandler { Public Object Create (object parent, Object configcontext, System.Xml.XmlNode section) { return section;}} }
XmlNode roles= System.Configuration.ConfigurationManager.GetSection ("roles") as XmlNode; Response.Write (Roles. ChildNodes [0]. attributes["username"]. InnerText);
Can also be configured as an entity
Using system;using system.collections.generic;using system.linq;using system.web;namespace ebuy.chapter3.ntier.webui{public class RolesConfig:System.Configuration.IConfigurationSectionHandler { Public Object Create (object parent, Object configcontext, System.Xml.XmlNode section) { var list=new list<role> (); for (int i=0;i<section. childnodes.count;i++) { list. ADD (New Role () { Username =section. Childnodes[i]. attributes["username"]. InnerText, Password =section. Childnodes[i]. attributes["Password"]. InnerText}); } return list; } } public class Role {public string Username {get; set;} public string Password{get;set;}} }
var roles = System.Configuration.ConfigurationManager.GetSection ("Roles") as List<ebuy.chapter3.ntier.webui.role >; Response.Write (Roles. First (). Username);
NET Web. config Single solution (additional configuration introduction)