标签:
在开发中经常会遇到这样的情况,在部署程序时为了保密起见并不将源代码随项目一同发布,而我们开发时的环境与部署环境可能不一致(比如数据库不一样),如果在代码中保存这些配置这些信息部署时需要到用户那里更改代码再重新编译,这种部署方式非常麻烦。在.net中提供了一种便捷的保存项目配置信息的办法,那就是利用配置文件,配置文件的文件后缀一般是.config,在asp.net中配置文件名一般默认是web.config。每个web.config文件都是基于XML的文本文件,并且可以保存到Web应用程序中的任何目录中。在发布Web应用程序时web.config文件并不编译进dll文件中。如果将来客户端发生了变化,仅仅需要用记事本打开web.config文件编辑相关设置就可以重新正常使用,非常方便。 本篇要讲述的知识如下: 配置文件的查找优先级 配置文件节点说明 配置文件的操作
配置文件的查找优先级
<appSettings> <!--允许上传的图片格式类型--> <add key="ImageType" value=".jpg;.bmp;.gif;.png;.jpeg"/> <!--允许上传的文件类型--> <add key="FileType" value=".jpg;.bmp;.gif;.png;.jpeg;.pdf;.zip;.rar;.xls;.doc"/> </appSettings>
对于<appSettings>节点中的值可以按照key来进行访问,以下就是一个读取key值为“FileType”节点值的例子: string fileType=ConfigurationManager.AppSettings["FileType "];
<connectionStrings>节点 <connectionStrings>节点主要用于配置数据库连接的,我们可以<connectionStrings>节点中增加任意个节点来保存数据库连接字符串,将来在代码中通过代码的方式动态获取节点的值来实例化数据库连接对象,这样一旦部署的时候数据库连接信息发生变化我们仅需要更改此处的配置即可,而不必因为数据库连接信息的变化而需要改动程序代码和重新部署。 以下就是一个<connectionStrings>节点配置的例子:
<connectionStrings> <!--SQL Server数据库配置--> <add name="AspNetStudyConnectionString1" connectionString="Data Source=(local);Initial Catalog=AspNetStudy;User ID=sa;Password=sa"/> </connectionStrings>
在代码中我们可以这么实例化数据库连接对象:
//读取web.config节点配置 string connectionString = ConfigurationManager.ConnectionStrings["AspNetStudyConnectionString1"].ConnectionString; //实例化SqlConnection对象 SqlConnection connection = new SqlConnection(connectionString);
<httpHandlers> <add path="*.rules" verb="*" type="System.Web.HttpForbiddenHandler" validate="true"/> <add path="*.xoml" verb="*" type="System.ServiceModel.Activation.HttpHandler, System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" validate="false"/> <add path="trace.axd" verb="*" type="System.Web.Handlers.TraceHandler" validate="true"/> <add path="WebResource.axd" verb="GET" type="System.Web.Handlers.AssemblyResourceLoader" validate="true"/> <add path="*.axd" verb="*" type="System.Web.HttpNotFoundHandler" validate="true"/> <add path="*.aspx" verb="*" type="System.Web.UI.PageHandlerFactory" validate="true"/> <add path="*.ashx" verb="*" type="System.Web.UI.SimpleHandlerFactory" validate="true"/> <add path="*.asmx" verb="*" type="System.Web.Services.Protocols.WebServiceHandlerFactory, System.Web.Services, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" validate="false"/> <add path="*.rem" verb="*" type="System.Runtime.Remoting.Channels.Http.HttpRemotingHandlerFactory, System.Runtime.Remoting, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" validate="false"/> <add path="*.soap" verb="*" type="System.Runtime.Remoting.Channels.Http.HttpRemotingHandlerFactory, System.Runtime.Remoting, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" validate="false"/> <add path="*.asax" verb="*" type="System.Web.HttpForbiddenHandler" validate="true"/> <add path="*.ascx" verb="*" type="System.Web.HttpForbiddenHandler" validate="true"/> <add path="*.master" verb="*" type="System.Web.HttpForbiddenHandler" validate="true"/> <add path="*.skin" verb="*" type="System.Web.HttpForbiddenHandler" validate="true"/> <add path="*.browser" verb="*" type="System.Web.HttpForbiddenHandler" validate="true"/> <add path="*.sitemap" verb="*" type="System.Web.HttpForbiddenHandler" validate="true"/> <add path="*.dll.config" verb="GET,HEAD" type="System.Web.StaticFileHandler" validate="true"/> <add path="*.exe.config" verb="GET,HEAD" type="System.Web.StaticFileHandler" validate="true"/> <add path="*.config" verb="*" type="System.Web.HttpForbiddenHandler" validate="true"/> <add path="*.cs" verb="*" type="System.Web.HttpForbiddenHandler" validate="true"/> <add path="*.csproj" verb="*" type="System.Web.HttpForbiddenHandler" validate="true"/> <add path="*.vb" verb="*" type="System.Web.HttpForbiddenHandler" validate="true"/> <add path="*.vbproj" verb="*" type="System.Web.HttpForbiddenHandler" validate="true"/> <add path="*.webinfo" verb="*" type="System.Web.HttpForbiddenHandler" validate="true"/> <add path="*.licx" verb="*" type="System.Web.HttpForbiddenHandler" validate="true"/> <add path="*.resx" verb="*" type="System.Web.HttpForbiddenHandler" validate="true"/> <add path="*.resources" verb="*" type="System.Web.HttpForbiddenHandler" validate="true"/> <add path="*.mdb" verb="*" type="System.Web.HttpForbiddenHandler" validate="true"/> <add path="*.vjsproj" verb="*" type="System.Web.HttpForbiddenHandler" validate="true"/> <add path="*.java" verb="*" type="System.Web.HttpForbiddenHandler" validate="true"/> <add path="*.jsl" verb="*" type="System.Web.HttpForbiddenHandler" validate="true"/> <add path="*.ldb" verb="*" type="System.Web.HttpForbiddenHandler" validate="true"/> <add path="*.ad" verb="*" type="System.Web.HttpForbiddenHandler" validate="true"/> <add path="*.dd" verb="*" type="System.Web.HttpForbiddenHandler" validate="true"/> <add path="*.ldd" verb="*" type="System.Web.HttpForbiddenHandler" validate="true"/> <add path="*.sd" verb="*" type="System.Web.HttpForbiddenHandler" validate="true"/> <add path="*.cd" verb="*" type="System.Web.HttpForbiddenHandler" validate="true"/> <add path="*.adprototype" verb="*" type="System.Web.HttpForbiddenHandler" validate="true"/> <add path="*.lddprototype" verb="*" type="System.Web.HttpForbiddenHandler" validate="true"/> <add path="*.sdm" verb="*" type="System.Web.HttpForbiddenHandler" validate="true"/> <add path="*.sdmDocument" verb="*" type="System.Web.HttpForbiddenHandler" validate="true"/> <add path="*.mdf" verb="*" type="System.Web.HttpForbiddenHandler" validate="true"/> <add path="*.ldf" verb="*" type="System.Web.HttpForbiddenHandler" validate="true"/> <add path="*.exclude" verb="*" type="System.Web.HttpForbiddenHandler" validate="true"/> <add path="*.refresh" verb="*" type="System.Web.HttpForbiddenHandler" validate="true"/> <add path="*.svc" verb="*" type="System.ServiceModel.Activation.HttpHandler, System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" validate="false"/> <add path="*" verb="GET,HEAD,POST" type="System.Web.DefaultHttpHandler" validate="true"/> <add path="*" verb="*" type="System.Web.HttpMethodNotAllowedHandler" validate="true"/> </httpHandlers>
<httpHandlers> <add path="IPData/*.txt" verb="*" type="System.Web.HttpForbiddenHandler"/> </httpHandlers>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="HttpHandlersDemo.aspx.cs" Inherits="HttpHandlersDemo" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>httpHandlers节点的例子</title> </head> <body> <form id="form1" runat="server"> <div> <a href="IPData/IPData.txt" title="打开IPData/IPData.txt">打开IPData/IPData.txt</a> </div> </form> </body> </html>
<customErrors mode="On" defaultRedirect="GenericErrorPage.htm"> <error statusCode="403" redirect="403.htm" /> <error statusCode="404" redirect="404.htm" /> </customErrors>
using System; using System.Configuration; using System.Web; using System.Web.Configuration; /// <summary> /// ConfigurationOperator 的摘要说明 /// </summary> public class ConfigurationOperator:IDisposable { private Configuration config; ConfigurationOperator():this(HttpContext.Current.Request.ApplicationPath) { } ConfigurationOperator(string path) { config = WebConfigurationManager.OpenWebConfiguration(path); } /// <summary> /// 设置应用程序配置节点,如果已经存在此节点,则会修改该节点的值,否则添加此节点 /// </summary> /// <param name="key">节点名称</param> /// <param name="value">节点值</param> void SetAppSetting() void SetAppSetting(string key, string value) { AppSettingsSection appSetting = (AppSettingsSection)config.GetSection("appSettings"); if (appSetting.Settings[key] == null)//如果不存在此节点,则添加 { appSetting.Settings.Add(key, value); } else//如果存在此节点,则修改 { appSetting.Settings[key].Value = value; } } /// <summary> /// 设置数据库连接字符串节点,如果不存在此节点,则会添加此节点及对应的值,存在则修改 /// </summary> /// <param name="key">节点名称</param> /// <param name="value">节点值</param> void SetConnectionString() void SetConnectionString(string key, string connectionString) { ConnectionStringsSection connectionSetting = (ConnectionStringsSection)config.GetSection("connectionStrings"); if (connectionSetting.ConnectionStrings[key] == null)//如果不存在此节点,则添加 { ConnectionStringSettings connectionStringSettings = new ConnectionStringSettings(key, connectionString); connectionSetting.ConnectionStrings.Add(connectionStringSettings); } else//如果存在此节点,则修改 { connectionSetting.ConnectionStrings[key].ConnectionString = connectionString; } } /// <summary> /// 保存所作的修改 /// </summary> void Save() { config.Save(); config = null; } void Dispose() { if (config != null) { config.Save(); } } }
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ConfigModifyDemo.aspx.cs" Inherits="ConfigModifyDemo" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>在部署后修改web.config的例子</title> </head> <body> <form id="form1" runat="server"> <div> <table border="0" cellpadding="0" cellspacing="0"> <tr> <td>类型</td> <td>名称</td> <td>值</td> </tr> <tr> <td> 程序配置</td> <td> <asp:TextBox ID="txtKey" runat="server"></asp:TextBox> <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtKey" ErrorMessage="*" Display="Dynamic"></asp:RequiredFieldValidator> </td> <td> <asp:TextBox ID="txtAppSetting" runat="server"></asp:TextBox> </td> </tr> <tr> <td> 数据库连接</td> <td> <asp:TextBox ID="txtConnectionName" runat="server"></asp:TextBox> <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="*" ControlToValidate="txtConnectionName" Display="Dynamic"></asp:RequiredFieldValidator> </td> <td style="height: 24px"> <asp:TextBox ID="txtConnectionString" runat="server"></asp:TextBox> </td> </tr> <tr> <td> <asp:Button ID="btnModify" runat="server" OnClick="btnModify_Click" Text="修改" /> </td> <td></td> <td></td> </tr> </table> </div> </form> </body> </html>
标签:
原文地址:http://www.cnblogs.com/zchunhua/p/5419248.html