web.config 详解

web.config文件是一个XML文本文件,他用来存储ASP.NET Web应用程序的配置信息。默认情况下会在根目录自动创建一个默认的Web.config文件,包括默认的的配置设置,所有的子目录都继承他的配置设置。如果你想修改子目录的配置设置,你可以在该子目录下新建一个web.config文件。他可以提供除从父目录继承的配置信息以外的配置信息,也可以重写或修改父目录中定义的设置。

在运行时对Web.config文件的修改不需要重启服务就可以生效(<processModel>例外)。

基本架构

<?xml version="1.0" encoding="utf-8"?>  
    <configuration>  
    <configSections></configSections>   
    <appSettings></appSettings >  
    <connectionStrings></connectionStrings >    
    <system.web></system.web>  
</configuration>  

1.根节点:configuration

2.二级节点:configSections,appSettings,connectionStrings,system.web等,是可有可无,根据需求而定

备注

1.web.config是XML文件,书写严格遵守XML规范

2.扩展名.config文件,在web应用程序中是不能被下载的,所以配置信息是安全的。

3.修改web.config文件会导致web应用程序重启,如果项目中使用session则信息会丢失。

4.web.config并不是web应用程序的必要文件。而且一个项目中的文件名"web.config"的文件可以不止一个

-----------------------------------------------------------------------------------------------

自定义web.config的文件配置过程分为两步

一是在配置文件顶部

<configSections></configSections>

标记之间声明配置节的名称和处理该节中配置数据的.NET Framework类的名称。

二是在

<configSections></configSection>

区域之后为声明的节做实际的配置设置。

实例1:log4net的配置

<?xml version="1.0"?>
<configuration>
    <configSections>
        <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
    </configSections>
    <log4net>
        ...
    </log4net>
</configuration>

示例2.configSection区内不但可以有section还可以有sectionGroup

<configuration>
    <configSections>
        <sectionGroup name="mySectionGroup">
            <section name="mySection" type="System.Configuration.NameValueSectionHandler" />
        </sectionGroup>
    </configSections>
    <mySectionGroup>
        <mySection>
            <add key="key1" value="value1" />
        </mySection>
    </mySectionGroup>
</configuration>

-----------------------------------------------------------------------------------------------

<appSettings></appSettings>此节可以定义应用程序的全局常量设置等信息。对于一些不确定设置,还可以让用户根据自己的实际情况自己设置。

1.定义一个连接字符串常量,并且在实际应用时可以修改连接字符串,不用修改程序代码

<appSettings>
    <add key="Connection" value="....." />
</appSettings>

2.定义一个错误重定向页面

<appSettings>
    <add key="ErrPage" value="Error.aspx" />
</appSettings>

3.程序读取方法

System.Configuration.ConfigurationManager.AppSetting.Get("KeyName");

-----------------------------------------------------------------------------------------------

<connectionStrings></connectionStrings>

配置数据库连接。在web.config中配置连接串是每个开发人员最常,最本能的举动。在connectionStrings可以指定不止一个。

<connectionStrings>
    <add name="foosun" connectionString="..." />
    <add name="helpke" connectionString="..." />
    <add name="collect"  connectionString="..." />
</connectionStrings>

配置读取

string cmsConStr = ConfigurationManager.ConnectionStrings["foosum"].ConnectionString;
string helpConStr = ConfigurationManager.ConnectionStrings["helpke"].ConnectionString;
string collConStr = ConfigurationManager.ConnectionStrings["collect"].ConnectionString;

完整的连接数据文件

<add name="LocalSqlServer" connectionString="Data Source=.\SQLExpress;Integrated Security=True;AttachDBFilename=|DataDirectory|TimeTracker.mdf;User Instance=true" />

1.Data Source

SqlConnectionStringBuilder的DataSource属性。DataSource可以由下列字符串代替"Server","Address","network address";

2.Intrgrated Security

当为true是,使用当前的Windows帐号凭据进行身份验证,为false时,需要在连接中指定用户ID和密码

3.AttachDBFilename

指定连接打开的时候动态附加到服务器上的数据库文件的位置。这个属性可以接受完整路径和相对路径。

4.User Instance

该值指示是否将连接默认的SQL Server Express 实例重定向到调用方账户之下运行并且在运行时启动实力。

connectionString一些细节

1.修改端口

<connectionStrings>
    <add name="Collect" connectionString="server=127.0.0.1,1435...." />
</connectionStrings>

2.如果我们的web数据库在255.255.255.1服务器的DBBACK实例下

<connectionStrings>
    <add name="Collect" connectionString="server=255.255.55.1\DBBACK;...." />
</connectionStrings>

-----------------------------------------------------------------------------------------------

web.config文件中的<system.webServer></system.webServer>节用于指定适用与Web应用程序的IIS7.0设置。system.webServer节中的某些设置只适用与IIS7.0集成模式,而不适用于经典模式。具体而言,如果应用程序正在经典模式下运行,则会忽略web.config文件的system.webServer节中指定的所有托管代码模块和处理程序。与IIS的早期版本相同,托管代码模块和处理程序必须在system.web节的httpModules和httpHandlers元素中的定义。

配置默认文件

<configuration>
    <system.webServer>
        <defaultDocument>
            <files>
                <add value="default.aspx" />
            </files>
        </defaultDocument>
    </system.webServer>
</configuration>

注册托管代码

<configuration>
    <system.webServer>
        <modules>
            <add name="CustomModule" type="Sample.CustomModule" />
        </modules>
    </system.webServer>
</configuration>

配置自定义响应标头

<configuration>
    <system.webServer>
          <httpProtocol>
               <customHeaders>
                    <add name="CustomHeader" value="CustomHeader" />
                </customHeaders>
           </httpProtocol>
    </system.webServer>
</configuration>    

-----------------------------------------------------------------------------------------------

system.web 下的节

1.

<compilation defaultLanguage="c#" debug="true" />

 Ⅰ.default language: 定义后台代码语言,可以选择C#和VB.net两种语言

 Ⅱ.debug:为true时,启动aspx调试;为false不启动aspx调试,因而可以提高应用程序运行时的性能。一般程序员在开发时设置为true,交给客户时设置为false.

2.

<customErrors mode="RemoteOnly" defaultRedirect="error.apsx">

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

参考:http://www.it118.org/specials/321869dd-98cb-431b-b6d2-82d973cd739d/4697cb26-0713-4f8b-8e1a-088b9f9c4dae.htm

 

posted @ 2014-03-31 16:47  yumuxu  阅读(604)  评论(0编辑  收藏  举报