---
title: "web。config"
author: "wfdoublext"
date: "2008-06-27T10:41:53Z"
tags: ["ASP", "ASP.net", "BBS", "Web", "应用服务器"]
slug: "web-config"
draft: false
lastmod: "2008-06-27T10:41:53Z"
id: 208523
url: "https://skyofdawn.iteye.com/blog/208523"
---

一、认识Web.config文件 网管下载dl.bitscn.com

    Web.config文件是一个XML文本文件，它用来储存 ASP.NET Web 应用程序的配置信息（如最常用的设置ASP.NET Web 应用程序的身份验证方式），它可以出现在应用程序的每一个目录中。当你通过VB.NET新建一个Web应用程序后，默认情况下会在根目录自动创建一个默认的Web.config文件，包括默认的配置设置，所有的子目录都继承它的配置设置。如果你想修改子目录的配置设置，你可以在该子目录下新建一个Web.config文件。它可以提供除从父目录继承的配置信息以外的配置信息，也可以重写或修改父目录中定义的设置。 中国网管论坛bbs.bitsCN.com

    在运行时对Web.config文件的修改不需要重启服务就可以生效（注：&lt;processModel&gt; 节例外）。当然Web.config文件是可以扩展的。你可以自定义新配置参数并编写配置节处理程序以对它们进行处理。 网管u家[u.bitscn@com](mailto:u.bitscn@com)

    二、web.config配置文件（默认的配置设置）以下所有的代码都应该位于 网管联盟bitsCN\_com

    &lt;configuration&gt; &lt;system.web&gt; 中国网管联盟bitsCN.com

    和

网管联盟bitsCN\_com

    &lt;/system.web&gt; &lt;/configuration&gt;

中国网管论坛bbs.bitsCN.com

    之间，出于学习的目的下面的示例都省略了这段XML标记

网管u家u.bitsCN.com

    1、&lt;authentication&gt; 节 网管u家[u.bitscn@com](mailto:u.bitscn@com)

    作用：配置 ASP.NET 身份验证支持（为Windows、Forms、PassPort、None四种）。该元素只能在计算机、站点或应用程序级别声明。&lt;authentication&gt; 元素必需与&lt;authorization&gt; 节配合使用。 网管网www\_bitscn\_com

    示例：

网管u家[u.bitscn@com](mailto:u.bitscn@com)

    以下示例为基于窗体（Forms）的身份验证配置站点，当没有登陆的用户访问需要身份验证的网页，网页自动跳转到登陆网页。

网管联盟bitsCN\_com

    &lt;authentication mode="Forms" &gt; &lt;forms loginUrl="logon.aspx" name=".FormsAuthCookie"/&gt;

中国网管联盟bitsCN.com

    &lt;/authentication&gt; 网管联盟[bitsCN@com](mailto:bitsCN@com)

    其中元素loginUrl表示登陆网页的名称，name表示Cookie名称 网管u家u.bitsCN.com

    2、&lt;authorization&gt; 节 网管u家u.bitsCN.com

    作用：控制对 URL 资源的客户端访问（如允许匿名用户访问）。此元素可以在任何级别（计算机、站点、应用程序、子目录或页）上声明。必需与&lt;authentication&gt; 节配合使用。

网管bitscn\_com

    示例：以下示例禁止匿名用户的访问 网管网www\_bitscn\_com

    &lt;authorization&gt; &lt;deny users="？"/&gt; &lt;/authorization&gt;

网管bitscn\_com

    注：你可以使用user.identity.name来获取已经过验证的当前的用户名；可以使用web.Security.FormsAuthentication.RedirectFromLoginPage方法将已验证的用户重定向到用户刚才请求的页面。具体的实例请参考： 网管网www\_bitscn\_com

    Forms验证 [http://www.fanvb.net/websample/dataauth.aspx](http://www.fanvb.net/websample/dataauth.aspx) 网管下载dl.bitscn.com

    3、&lt;compilation&gt;节

网管论坛bbs\_bitsCN\_com

    作用：配置 ASP.NET 使用的所有编译设置。默认的debug属性为“True”。在程序编译完成交付使用之后应将其设为True（Web.config文件中有详细说明，此处省略示例）

网管论坛bbs\_bitsCN\_com

    4、&lt;customErrors&gt;

网管bitscn\_com

    作用：为 ASP.NET 应用程序提供有关自定义错误信息的信息。它不适用于 XML Web services 中发生的错误。 网管下载dl.bitscn.com

    示例：当发生错误时，将网页跳转到自定义的错误页面。 网管联盟[bitsCN@com](mailto:bitsCN@com)

    &lt;customErrors defaultRedirect="ErrorPage.aspx" mode="RemoteOnly"&gt; &lt;/customErrors&gt;

网管联盟[bitsCN@com](mailto:bitsCN@com)

    其中元素defaultRedirect表示自定义的错误网页的名称。mode元素表示：对不在本地 Web 服务器上运行的用户显示自定义（友好的）信息。 网管联盟[bitsCN@com](mailto:bitsCN@com)

    5、&lt;httpRuntime&gt;节

网管u家[u.bitscn@com](mailto:u.bitscn@com)

    作用：配置 ASP.NET HTTP 运行库设置。该节可以在计算机、站点、应用程序和子目录级别声明。 网管u家u.bitsCN.com

    示例：控制用户上传文件最大为4M，最长时间为60秒，最多请求数为100

网管论坛bbs\_bitsCN\_com

    &lt;httpRuntime maxRequestLength="4096" executionTimeout="60" appRequestQueueLimit="100"/&gt;

网管论坛bbs\_bitsCN\_com

    6、 &lt;pages&gt; 中国网管联盟bitsCN.com

    作用：标识特定于页的配置设置（如是否启用会话状态、视图状态，是否检测用户的输入等）。&lt;pages&gt;可以在计算机、站点、应用程序和子目录级别声明。

网管u家[u.bitscn@com](mailto:u.bitscn@com)

    示例：不检测用户在浏览器输入的内容中是否存在潜在的危险数据（注：该项默认是检测，如果你使用了不检测，一要对用户的输入进行编码或验证），在从客户端回发页时将检查加密的视图状态，以验证视图状态是否已在客户端被篡改。（注：该项默认是不验证）

网管联盟[bitsCN@com](mailto:bitsCN@com)

    &lt;pages buffer="true" enableViewStateMac="true" validateRequest="false"/&gt;

网管联盟bitsCN\_com

    7、&lt;sessionState&gt;

网管bitscn\_com

    作用：为当前应用程序配置会话状态设置（如设置是否启用会话状态，会话状态保存位置）。 网管u家[u.bitscn@com](mailto:u.bitscn@com)

    示例：

中国网管联盟bitsCN.com

    &lt;sessionState mode="InProc" cookieless="true" timeout="20"/&gt; &lt;/sessionState&gt;

中国网管联盟bitsCN.com

    注：

网管下载dl.bitscn.com

    mode="InProc"表示：在本地储存会话状态（你也可以选择储存在远程服务器或SAL服务器中或不启用会话状态）

网管下载dl.bitscn.com

    cookieless="true"表示：如果用户浏览器不支持Cookie时启用会话状态（默认为False） 网管联盟[bitsCN@com](mailto:bitsCN@com)

    timeout="20"表示：会话可以处于空闲状态的分钟数 网管网www\_bitscn\_com

    8、&lt;trace&gt;

网管下载dl.bitscn.com

    作用：配置 ASP.NET 跟踪服务，主要用来程序测试判断哪里出错。

网管u家[u.bitscn@com](mailto:u.bitscn@com)

    示例：以下为Web.config中的默认配置： 网管联盟[bitsCN@com](mailto:bitsCN@com)

    &lt;trace enabled="false" requestLimit="10" pageOutput="false" traceMode="SortByTime" localOnly="true" /&gt;

网管联盟bitsCN\_com

    注：

网管论坛bbs\_bitsCN\_com

    enabled="false"表示不启用跟踪；requestLimit="10"表示指定在服务器上存储的跟踪请求的数目

中国网管论坛bbs.bitsCN.com

    pageOutput="false"表示只能通过跟踪实用工具访问跟踪输出； 网管论坛bbs\_bitsCN\_com

    traceMode="SortByTime"表示以处理跟踪的顺序来显示跟踪信息 网管联盟[bitsCN@com](mailto:bitsCN@com)

    localOnly="true" 表示跟踪查看器 （trace.axd） 只用于宿主 Web 服务器

网管联盟bitsCN\_com

    三、自定义Web.config文件配置节 网管联盟bitsCN\_com

    自定义Web.config文件配置节过程分为两步。 网管u家[u.bitscn@com](mailto:u.bitscn@com)

    一是在在配置文件顶部 &lt;configSections&gt; 和 &lt;/configSections&gt;标记之间声明配置节的名称和处理该节中配置数据的 .NET Framework 类的名称。

网管bitscn\_com

    二是在 &lt;configSections&gt; 区域之后为声明的节做实际的配置设置。 网管u家u.bitsCN.com

    示例：创建一个节存储数据库连接字符串 网管网www\_bitscn\_com

    &lt;configuration&gt; &lt;configSections&gt; &lt;section name="appSettings" type="System.Configuration.NameValueFileSectionHandler， System， Version=1.0.3300.0， Culture=neutral， PublicKeyToken=b77a5c561934e089"/&gt; &lt;/configSections&gt;

网管论坛bbs\_bitsCN\_com

    &lt;appSettings&gt; &lt;add key="scon" value="server=a；database=northwind；uid=sa；pwd=123"/&gt; &lt;/appSettings&gt; 网管bitscn\_com

    &lt;system.web&gt;……

网管联盟[bitsCN@com](mailto:bitsCN@com)

    &lt;/system.web&gt; &lt;/configuration&gt;

网管bitscn\_com

    四、访问Web.config文件

网管网www\_bitscn\_com

    你可以通过使用ConfigurationSettings.AppSettings 静态字符串集合来访问 Web.config 文件示例：获取上面例子中建立的连接字符串。 网管网www\_bitscn\_com

    Dim sconstr As String = ConfigurationSettings.AppSettings（"SconStr"） 网管网www\_bitscn\_com

    Dim scon = New SqlConnection（sconstr） 网管论坛bbs\_bitsCN\_com  
【转自[www.bitsCN.com](http://www.bitsCN.com)】