现在的位置: 首页 > 综合 > 正文

Web.config配置文件详解(新手必看)

2013年10月13日 ⁄ 综合 ⁄ 共 9409字 ⁄ 字号 评论关闭

Web.config配置文件详解(新手必看)

花了点时间整理了一下ASP.NET Web.config配置文件的基本使用方法。很适合新手参看,由于Web.config在使用很灵活,可以自定义一些节点。所以这里只介绍一些比较常用的节点。

<?xml version="1.0"?>

<!--注意: 除了手动编辑此文件以外,您还可以使用 Web 管理工具来配置应用程序的设置。可以使用 Visual Studio 中的“网站”->“Asp.Net 配置”选项。

设置和注释的完整列表在 machine.config.comments 中,该文件通常位于 "Windows"Microsoft.Net"Framework"v2.x"Config 中。-->

 <!--Webconfig文件是一个xml文件,configuration是xml文件的根节点,由于xml文件的根节点只能有一个,所以Webconfig的所有配置都是在这个节点内进行的。-->

<configuration>

  <!--指定配置节和命名空间声明。clear:移除对继承的节和节组的所有引用,只允许由当前 section 和 sectionGroup 元素添加的节和节组。remove:移除对继承的节和节组的引用。

 section:定义配置节处理程序与配置元素之间的关联。sectionGroup:定义配置节处理程序与配置节之间的关联。-->

 <configSections>

    <sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">

      <sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">

        <section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication"/>

      </sectionGroup>

    </sectionGroup>

    <section name="rewriter" type="Intelligencia.UrlRewriter.Configuration.RewriterConfigurationSectionHandler, Intelligencia.UrlRewriter" />

 </configSections>

 

 <!--appSettings是应用程序设置,可以定义应用程序的全局常量设置等信息-->

     <appSettings>

 <add key="1" value="1" />

 <add key="gao" value="weipeng" />

 </appSettings>

 

 <!--连接字符串设置-->

 <connectionStrings>

    <add name="ConnString" connectionString="Data Source=GAO;Initial Catalog=HBWXDate;User ID=sa;password=sa"></add>

    <add name="111" connectionString="11111" />

 </connectionStrings>

 

 <!--指定应用子配置设置的资源,并锁定配置设置,以防止它们被子配置文件重写。page指定应用包含的配置设置的资源.allowOverride是否允许配置文件的重写,提高配置文件的安全性-->

 <location path="Default.aspx" allowOverride="false">

    <!--控制asp.net运行时的行为-->

<system.web>

    <!--identity控制web应用程序的身份验证标识.-->

    <identity impersonate="false" />

   

    <!--标识特定于页的配置设置(如是否启用会话状态、视图状态,是否检测用户的输入等)。<pages>可以在计算机、站点、应用程序和子目录级别声明.

    这里的几个属性的意思是默认主页为Index,主题是Default,不检测用户在浏览器输入的内容中是否存在潜在的危险数据(注:该项默认是检测,如果你使用了不检测,一要对用户的输入进行编码或验证),在从客户端回发页时将检查加密的视图状态,以验证视图状态是否已在客户端被篡改。(注:该项默认是不验证)禁用ViewState-->

    <pages masterPageFile="Index" theme="Default" buffer="true" enableViewStateMac="true" validateRequest="false" enableViewState="false">

      <!--controls 元素定义标记前缀所在的 register 指令和命名空间的集合-->

      <controls></controls>

      <!--将在程序集预编译期间使用的导入指令的集合-->

      <namespaces></namespaces>

    </pages>

     <!--默认错误页设置,mode:具有On,Off,RemoteOnly 3种状态
customErrors mode="On" 或 "RemoteOnly" 以启用自定义错误信息,或设置为 "Off" 以禁用自定义错误信息。
为每个要处理的错误添加 <error> 标记。
"On" 始终显示自定义(友好的)信息。
"Off" 始终显示详细的 ASP.NET 错误信息。
"RemoteOnly" 只对不在本地 Web 服务器上运行的
用户显示自定义(友好的)信息。出于安全目的,建议使用此设置,以便
不向远程客户端显示应用程序的详细信息。
defaultRedirect:用于出现错误时重定向的URL地址
-->

    <customErrors defaultRedirect="Err.html" mode="RemoteOnly">

      <!--特殊代码编号的错误从定向文件-->

      <error statusCode="403" redirect="NoAccess.htm" />

      <error statusCode="404" redirect="FileNotFound.htm" />

    </customErrors>

   

    <!--配置调试和跟踪:下面配置的意思是启动调试(默认),捕获跟踪信息,要缓存的跟踪请求个数(15),跟踪结果的排列顺序-->

    <trace enabled="true" localOnly="false" pageOutput="true" requestLimit="15" traceMode="SortByCategory"/>

   

    <!-- 设置 compilation debug="true" 以启用 ASPX 调试。否则,将此值设置为
false 将提高此应用程序的运行时性能。
设置 compilation debug="true" 以将调试符号(.pdb 信息)
插入到编译页中。因为这将创建执行起来
较慢的大文件,所以应该只在调试时将此值设置为 true,而在所有其他时候都设置为
false。有关更多信息,请参考有关调试 ASP.NET 文件的文档。
设置默认的开发语言C#。batch是否支持批处理
-->

    <compilation debug="true" defaultLanguage="c#" batch="false">

      <assemblies>

        <!--加的程序集引用,每添加一个程序集,就表示你的应用程序已经依赖了一个程序集,你就可以在你的应用程序中使用了-->

        <add assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>

        <add assembly="System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>

        <add assembly="System.Web.Extensions.Design, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

        <add assembly="System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>

        <add assembly="System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>

        <add assembly="System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>

        <add assembly="System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>

        <add assembly="System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>

        <add assembly="System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>

        <add assembly="System.Web.Services, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>

        <add assembly="System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>

      </assemblies>

      <!--定义用于编译自定义资源文件的生成提供程序的集合。-->

      <buildProviders>

        <!---->

        <add extension=".aspx" type="System.Web.Compilation.PageBuildProvider"/>

        <add extension=".ascx" type="System.Web.Compilation.UserControlBuildProvider"/>

        <add extension=".master" type="System.Web.Compilation.MasterPageBuildProvider"/>

        <add extension=".asmx" type="System.Web.Compilation.WebServiceBuildProvider"/>

        <add extension=".ashx" type="System.Web.Compilation.WebHandlerBuildProvider"/>

        <add extension=".soap" type="System.Web.Compilation.WebServiceBuildProvider"/>

        <add extension=".resx" type="System.Web.Compilation.ResXBuildProvider"/>

        <add extension=".resources" type="System.Web.Compilation.ResourcesBuildProvider"/>

        <add extension=".wsdl" type="System.Web.Compilation.WsdlBuildProvider"/>

        <add extension=".xsd" type="System.Web.Compilation.XsdBuildProvider"/>

        <add extension=".rdlc" type="Microsoft.Reporting.RdlBuildProvider, Microsoft.ReportViewer.Common, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>

      </buildProviders>

    </compilation>

   

      <!--通过 <authentication> 节可以配置 ASP.NET 使用的 安全身份验证模式,以标识传入的用户。Windows: IIS 根据应用程序的设置执行身份验证
(基本、简要或集成 Windows),在 IIS 中必须禁用匿名访问。,Forms: 使用基于窗体的验证方式,为用户提供一个输入凭据的自定义窗体(Web 页),然后
在您的应用程序中验证他们的身份,用户凭据标记存储在 Cookie 中。Passport: 采用Passport 通过 Microsoft 的集中身份验证服务执行的,
它为成员站点提供单独登录和核心配置文件服务。,None: 不采用任何验证方式
-->

    <authentication mode="Forms">

      <!--Name: 指定完成身份验证的Http cookie的名称.LoginUrl: 如果未通过验证或超时后重定向的页面URL,一般为登录页面,让用户重新登录。Protection: 指定 cookie数据的保护方式. 

      可设置为:All表示加密数据,并进行有效性验证两种方式,None表示不保护Cookie,Encryption表示对Cookie内容进行加密,validation表示对Cookie内容进行有效性验证,TimeOut: 指定Cookie的失效时间. 超时后要重新登录。-->

      <forms name=".ASPXUSERDEMO" loginUrl="Login.aspx" protection="All" timeout="30"/>
    </authentication>

    <!--控制对 URL 资源的客户端访问(如允许匿名用户访问)。此元素可以在任何级别(计算机、站点、应用程序、子目录或页)上声明。必需与<authentication> 节配合使用。此处的意思是对匿名用户不进行身份验证。拒绝用户weipeng-->

   <!-- 授权 此节设置应用程序的授权策略。可以允许或拒绝不同的用户或角色访问应用程序资源。通配符: "*" 表示任何人,"?" 表示匿名(未经身份验证的)用户。-->  
    <
authorization> 
<
allow users="*"/><!-- 允许所有用户 -->
     <deny users="weipeng"/>

    <!--<allow users="[逗号分隔的用户列表]"
roles="[逗号分隔的角色列表]"/>
<deny users="[逗号分隔的用户列表]"
roles="[逗号分隔的角色列表]"/>
-->
 

      <allow users="aa" roles="aa" />

    </authorization>

    <!--站点全球化设置,requestEncoding: 它用来检查每一个发来请求的编码.responseEncoding: 用于检查发回的响应内容编码.fileEncoding:用于检查aspx,asax等文件解析的默认编码,默认的编码是utf-8-->

    <globalization requestEncoding="gb2312" responseEncoding="gb2312" fileEncoding="gb2312" />

    <!--会话状态设置。mode: 分为off,Inproc,StateServer,SqlServer几种状态 mode = InProc 存储在进程中特点:具有最佳的性能,速度最快,但不能跨多台服务器存储共享.mode = "StateServer" 存储在状态服务器中特点:当需要跨服务器维护用户会话信息时,使用此方法。但是信息存储在状态服务器上,一旦状态服务器出现故障,信息将丢失. mode="SqlServer" 存储在sql server中特点:工作负载会变大,但信息不会丢失

    stateConnectionString :指定asp.net应用程序存储远程会话状态的服务器名,默认为本机。sqlConnectionString:当用会话状态数据库时,在这里设置连接字符串。Cookieless:设置为flase时,表示使用cookie会话状态来标识客户.timeout表示会话超时时间。-->

    <sessionState mode="InProc" stateConnectionString="tcpip=127.0.0.1:42424" sqlConnectionString="data source=127.0.0.1;Trusted_Connection=yes" cookieless="false" timeout="20"></sessionState>

    <!--为 ASP.NET 应用程序配置页的视图状态设置。设置要存储在页历史记录中的项数。-->

    <sessionPageState historySize="9"/>   

    <!--配置asp.net http运行库的设置。可以在计算机,站点,应用程序和子目录级别声明

    允许最多的请求个数100,最长允许执行请求时间为80秒,控制用户上传文件的大小,默认是4M。useFullyQualifiedRedirectUrl客户端重定向不需要被自动转换为完全限定格式。-->

    <httpRuntime appRequestQueueLimit="100" executionTimeout="80" maxRequestLength="40960" useFullyQualifiedRedirectUrl="false"/>

    <!--httpModules在一个应用程序内配置 HTTP 模块。-->

    <httpModules>

      <add name="OutputCache" type="System.Web.Caching.OutputCacheModule" />

      <add name="Session" type="System.Web.SessionState.SessionStateModule" />

      <add name="WindowsAuthentication" type="System.Web.Security.WindowsAuthenticationModule" />

      <add name="FormsAuthentication" type="System.Web.Security.FormsAuthenticationModule" />

      <add name="PassportAuthentication" type="System.Web.Security.PassportAuthenticationModule" />

      <add name="RoleManager" type="System.Web.Security.RoleManagerModule" />

      <add name="UrlAuthorization" type="System.Web.Security.UrlAuthorizationModule" />

      <add name="FileAuthorization" type="System.Web.Security.FileAuthorizationModule" />

      <add name="AnonymousIdentification" type="System.Web.Security.AnonymousIdentificationModule" />

      <!--自定义的URL重写,type基本上就是dll名-->

      <add name="UrlRewriter" type="Intelligencia.UrlRewriter.RewriterHttpModule, Intelligencia.UrlRewriter" />

      <add name="Profile" type="System.Web.Profile.ProfileModule" />

    </httpModules> 

    <!--httpHandlers用于根据用户请求的URL和HTTP谓词将用户的请求交给相应的处理程序。可以在配置级别的任何层次配置此节点,也就是说可以针对某个特定目录下指定的特殊文件进行特殊处理。

    add:指定映射到处理程序的谓词/路径。clear:移除当前已配置或已继承的所有处理程序映射。remove:移除映射到处理程序的谓词/路径。remove 指令必须与前一个 add 指令的谓词/路径组合完全匹配。该指令不支持通配符。-->

    <httpHandlers>

      <remove verb="

抱歉!评论已关闭.