为 IIS 7.0 配置 <system.webServer>

本文详细介绍了在Web.config文件中system.webServer节的配置,包括默认文档设置、托管代码模块注册和自定义响应标头配置,旨在帮助开发者理解和应用这些设置以优化Web应用程序的性能。

 Web.config 文件中的 system.webServer 节用于指定适用于 Web 应用程序的 IIS 7.0 设置。system.WebServer 是 configuration 节的子级。有关更多信息,请参见 IIS 7.0: system.webServer Section Group (IIS Settings Schema)(IIS 7.0:system.webServer 节组(IIS 设置架构))。

下面是可以在 system.WebServer 配置组中进行的 Web 服务器设置的示例:

  • 当请求未包含特定资源时,Web 服务器返回给客户端的默认文档(defaultDocument 元素)。

  • 响应的压缩设置(httpCompression 元素)。

  • 自定义标头(httpProtocol 节的 customHeaders 元素)。

  • 模块(modules 元素)。

  • 处理程序(handlers 元素)。

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

本主题阐释需要修改 system.webServer 节的三个常见配置任务:

  • 添加默认文件,以便在请求 URL 未包含特定的文件时,提供该默认文件。

  • 注册托管代码模块。

  • 添加自定义响应标头。

当请求 URL 未包含 Web 应用程序的特定文件时,IIS 7.0 将提供一个默认文件。

配置默认文件

  1. 如果应用程序没有 Web.config 文件,请使用 Visual Studio 或文本编辑器创建该文件。

    有关更多信息,请参见编辑 ASP.NET 配置文件

  2. 如果 Web.config 文件尚未包含 system.webServer 节,请在 configuration 元素中创建该节,如下面的示例所示:

    <configuration>
      <system.webServer>
      </system.webServer>
    </configuration>
  3. 在 system.webServer 元素内,创建一个 defaultDocument 元素。

  4. 在 defaultDocument 元素内,创建一个 files 元素。

  5. 在 files 元素内创建一个 add 元素,并在 value 属性内指定默认文件的路径和名称。

    下面的示例演示了一个 system.webServer 节,该节配置为提供 Products.aspx 文件作为默认文件。

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

每次请求时都会调用托管代码模块,通过该模块可对请求或响应进行自定义。

配置自定义托管代码模块

  1. 如果应用程序没有 Web.config 文件,请使用 Visual Studio 或文本编辑器创建该文件。

    有关更多信息,请参见编辑 ASP.NET 配置文件

  2. 如果 Web.config 文件尚未包含 system.webServer 节,请在 configuration 元素中创建该节,如下面的示例所示:

    <configuration>
      <system.webServer>
      </system.webServer>
    </configuration>
  3. 在 system.webServer 元素内,创建一个 modules 元素。

  4. 在 modules 元素内创建一个 add 元素,并在 name 和 type 属性中指定自定义模块。

    实际的名称和类型取决于要添加的模块。下面的示例演示如何添加名为 CustomModule 的自定义模块,该模块将实现为类型 Samples.CustomModule。

    <configuration>
      <system.webServer>
        <modules>      <add name="CustomModule" type="Samples.CustomModule" />    </modules>
      </system.webServer>
    </configuration>
  5. 向模块注册中添加 precondition 属性,并将其值设置为 managedHandler。

    此前置条件会导致仅在请求 ASP.NET 应用程序资源(例如 .aspx 文件或托管处理程序)时才调用该模块。该资源中不包括静态文件(例如 .htm 文件)。

    其 configuration 节将类似于以下示例。

     
              
    <configuration>
      <system.webServer>
        <modules>
          <add name="CustomModule" type="Samples.CustomModule" 
               precondition="managedHandler" />
        </modules>
        <defaultDocument>
          <files>
            <add value="Products.aspx" />
          </files>
        </defaultDocument>
      </system.webServer>
    </configuration>
配置自定义响应标头
 
利用自定义响应标头,可向浏览器发送应用程序特定的信息。例如,可以添加 Content-Language 标头来描述网页正文中使用的语言。若要执行此操作,请提供一个或多个语言和国家/地区值,例如 en-US(美国英语)或 en-GB(英国英语)。

配置自定义响应标头

  1. 如果应用程序没有 Web.config 文件,请使用 Visual Studio 或文本编辑器创建该文件。

    有关更多信息,请参见编辑 ASP.NET 配置文件

  2. 如果 Web.config 文件尚未包含 system.webServer 节,请在 configuration 元素中创建该节,如下面的示例所示:

<configuration>
  <system.webServer>
  </system.webServer>
</configuration>
  1. 在 system.webServer 元素内,创建一个 httpProtocol 元素。

  2. 在 httpProtocol 元素内,创建一个 customHeaders 元素。

  3. 在 customHeaders 元素内创建一个 add 标记,并在 name 和 value 属性中指定自定义标头。

    实际的名称和类型将取决于该标头在应用程序中的功能。下面的示例演示如何添加名为 CustomHeader 且值为 CustomHeader 的自定义标头。

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

     

<?xml version="1.0" encoding="utf-8"?> <!-- 有关如何配置 ASP.NET 应用程序的详细信息,请访问 https://go.microsoft.com/fwlink/?LinkId=169433 --> <configuration> <connectionStrings> <add name="DB-NetShopsConnectionString" connectionString="Data Source=.\MSSQLSERVER2012;Initial Catalog=DB-NetShops;User ID=sa;Password=your_password;Integrated Security=False" providerName="System.Data.SqlClient" /> </connectionStrings> <system.web> <!-- 添加全局cookie设置 --> <httpCookies httpOnlyCookies="true" /> <compilation debug="true" targetFramework="4.7.2" /> <httpRuntime targetFramework="4.7.2" maxRequestLength="20480" executionTimeout="300" requestValidationMode="4.7.2" /> <!-- 移除httpOnlyCookies属性 --> <sessionState mode="InProc" timeout="20" /> <authentication mode="Forms"> <!-- 移除httpOnlyCookies属性 --> <forms loginUrl="~/WebForm1.aspx" defaultUrl="~/WebForm2.aspx" timeout="20" /> </authentication> <globalization culture="zh-CN" uiCulture="zh-CN" requestEncoding="utf-8" responseEncoding="utf-8" /> <customErrors mode="RemoteOnly" defaultRedirect="~/Error.aspx"> <error statusCode="404" redirect="~/404.aspx" /> <error statusCode="500" redirect="~/500.aspx" /> </customErrors> <pages masterPageFile="~/Site.master"> <controls> <add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/> </controls> </pages> </system.web> <system.webServer> <httpProtocol> <customHeaders> <add name="X-Content-Type-Options" value="nosniff" /> <add name="X-Frame-Options" value="SAMEORIGIN" /> <add name="X-Xss-Protection" value="1; mode=block" /> </customHeaders> </httpProtocol> <staticContent> <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="7.00:00:00" /> </staticContent> <security> <requestFiltering> <requestLimits maxAllowedContentLength="20971520" /> </requestFiltering> </security> </system.webServer> <system.codedom> <compilers> <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701" /> </compilers> </system.codedom> </configuration> 添加母版页代码设置,生成完整webconfig代码
06-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值