ASP.NET2.0 Web.config配置
ASP.NET2.0 Web.config配置 | |||||||||||
从web.config里提取appSettings里的配置值 | |||||||||||
<appSettings> <add key="pagetitle" value="Job Site Starter Kit (Ver.1.0)"></add> <add key="sitelogo" value="logo.gif"></add> <add key="advertiseemail" value="sales@somesite.com"></add> </appSettings> |
|||||||||||
利用ASP.NET2.0提供的一组API函数,您可以很容易的获取AppSettingsSection里所有的Keys/value组对 | |||||||||||
Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath); AppSettingsSection appSettings = (AppSettingsSection) config.GetSection("appSettings"); string[] appKeys = appSettings.Settings.AllKeys; for (int i = 0; i < appSettings.Settings.Count; i++) { //这里只进行简单的输出 Response.Write(appSettings.Settings[appKeys[i]].Value); Response.Write("<BR>"); } |
|||||||||||
Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath); AppSettingsSection appSettings = (AppSettingsSection)config.GetSection("appSettings"); string pateTitle= appSettings.Settings["pagetitle"].Value; //获取key为patetitle的value值 string siteLogo= appSettings.Settings["siteLogo"].Value; //获取key为sitelogo的value值 |
|||||||||||
对于数据库连接字符串,在ASP.NET2.0里提供了专门的配置节如下: | |||||||||||
<connectionStrings> <add name="connectionstring" connectionString="Data Source=SQLEXPRESS;AttachDbFilename=JsskDb.mdf; … .."/> <add name="MyProviderConnectionString" connectionString="Data Source=SQLEXPRESS;Integrated Security=True; … …"/> </connectionStrings> |
|||||||||||
这样我们很容易获取数据库连接字符串如下: | |||||||||||
Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath); ConnectionStringsSection conSection = (ConnectionStringsSection)config.GetSection("connectionstring "); ConnectionStringSettingsCollection conCollection = conSection.ConnectionStrings; foreach (ConnectionStringSettings conSetting in conCollection) { Response.Write(conSetting.ConnectionString); Response.Write("<BR>"); } |
|||||||||||
利用API函数,你同时还可以在代码里更改web.config数据库连接的配置的值 | |||||||||||
Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath); ConnectionStringsSection conSection = (ConnectionStringsSection)config.GetSection("connectionStrings"); conSection.ConnectionStrings["SQLConnectionString"].ConnectionString = "Data Source=SQLEXPRESS;Integrated Security=True; … …"; config.Save(); |
|||||||||||
配置文件中添加节 | |||||||||||
配置文件中添加节处理程序声明,该配置文件不必是添加自定义配置元素的配置文件,只要声明节处理程序的配置文件在配置文件的层次结构中位于较高的位置 section 元素的 type 属性必须与程序集清单匹配,否则将出现配置错误。程序集文件必须与定义它的 Web.config 文件位于相同的 ASP.NET 应用程序目录。 |
|||||||||||
<configuration> <!-- Configuration section-handler declaration area. --> <configSections> <sectionGroup name="myCustomGroup"> <section name="myCustomSection" type="MyConfigSectionHandler.MyHandler, MyCustomConfigurationHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" allowLocation="true" allowDefinition="Everywhere" /> </sectionGroup> <!-- Other <section> and <sectionGroup> elements. --> </configSections> <!-- Configuration section settings area. --> </configuration> |
|||||||||||
在 Web.config 文件的配置节设置区域中添加自定义配置元素。 | |||||||||||
<configuration> <!-- Configuration section-handler declaration area. --> <!-- Configuration section settings area. --> <myCustomGroup> <myCustomSection myAttrib1="Clowns"> <myChildSection myChildAttrib1="Zippy" myChildAttrib2="Michael Zawondy "/> </myCustomSection> </myCustomGroup> <!-- Other configuration settings, like <system.web> --> </configuration> |
|||||||||||
以编程方式访问自定义配置数据 | |||||||||||
protected void Button1_Click(object sender, EventArgs e) { MyConfigSectionHandler.MyHandler config = (MyConfigSectionHandler.MyHandler)System.Configuration.ConfigurationManager.GetSection( "myCustomGroup/myCustomSection"); StringBuilder sb = new StringBuilder(); sb.Append("<h2>Attributes in the myCustomSection Element:</h2>"); sb.AppendFormat("myAttrib1 = {0}<br/>", config.MyAttrib1.ToString()); sb.Append("<h2>Attributes in the myChildSection Element:</h2>"); sb.AppendFormat("myChildAttrib1 = {0}<br/>", config.MyChildSection.MyChildAttribute1.ToString()); sb.AppendFormat("myChildAttrib2 = {0}<br/>", config.MyChildSection.MyChildAttribute2.ToString()); Label1.Text = sb.ToString(); Label1.Visible = true; } |
|||||||||||