using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Web.Configuration; public partial class _Default : System.Web.UI.Page { protectedvoid Page_Load(object sender, EventArgs e) { } protectedvoid ShowMessage(string temp) { this.Response.Write("<script>alert('"+ temp +"')</script>"); } //获取 appsetting 的值 protectedvoid Get_AppSetting_Click(object sender, EventArgs e) { string message=System .Web.Configuration.WebConfigurationManager.AppSettings["message"]; this.ShowMessage(message); } //取得联接字符串的值 protectedvoid Get_Connection_Click(object sender, EventArgs e) { string cnn = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["cnn"].ConnectionString ; this.ShowMessage(cnn); } //用 xpath 方式获取config 中的一section protectedvoid Get_Section_Click(object sender, EventArgs e) { Configuration config; config = WebConfigurationManager.OpenWebConfiguration("~"); System.Web.Configuration.IdentitySection section; section =config.GetSection("system.web/identity") as System.Web.Configuration.IdentitySection; if (section !=null) { ShowMessage(section.Impersonate.ToString () ); } } //改变结点信息 protectedvoid Change_Section_Click(object sender, EventArgs e) { Configuration config; config = WebConfigurationManager.OpenMachineConfiguration("~"); System.Web.Configuration.IdentitySection section; section = config.GetSection("system.web/identity") as System.Web.Configuration.IdentitySection; if (section !=null) { section.Impersonate =!section.Impersonate ; config.Save(); ShowMessage("debug setting is now "+ section.Impersonate.ToString()); } } //获取外部数据 protectedvoid Get_External_Click(object sender, EventArgs e) { string cnn = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["cnn"].ConnectionString; this.ShowMessage(cnn); } }
cnn.config
<connectionStrings> <add name ="cnn" connectionString ="cnnvalue"/> </connectionStrings>
web.config
<?xml version="1.0"?> <!-- Note: As an alternative to hand editing this file you can use the web admin tool to configure settings for your application. Use the Website->Asp.Net Configuration option in Visual Studio. A full list of settings and comments can be found in machine.config.comments usually located in \Windows\Microsoft.Net\Framework\v2.x\Config --> <configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0"> <appSettings> <add key="message" value="hello world"/> </appSettings> <connectionStrings configSource ="cnn.config"> </connectionStrings> <system.web> <!-- Set compilation debug="true" to insert debugging symbols into the compiled page. Because this affects performance, set this value to true only during development. --> <compilation debug="true"/> <!-- The <authentication> section enables configuration of the security authentication mode used by ASP.NET to identify an incoming user. --> <authentication mode="Windows"/> <identity impersonate="true"/> <!-- The <customErrors> section enables configuration of what to do if/when an unhandled error occurs during the execution of a request. Specifically, it enables developers to configure html error pages to be displayed in place of a error stack trace. <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm"> <error statusCode="403" redirect="NoAccess.htm"/> <error statusCode="404" redirect="FileNotFound.htm"/> </customErrors> --> </system.web> </configuration>