如何:为 IIS 7.0 配置 节?

https://technet.microsoft.com/zh-cn/sysinternals/bb763179.aspx

https://www.cnblogs.com/tl2f/p/5016154.html

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 节的 httpModuleshttpHandlers 元素中定义。

本主题阐释需要修改 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>
  3. 在 system.webServer 元素内,创建一个 httpProtocol 元素。

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

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

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

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

请参见

任务

演练:在 IIS 7.0 中配置 ASP.NET 应用程序

概念

将 ASP.NET 应用程序从 IIS 6.0 迁移到 IIS 7.0

参考

configuration 元素(常规设置架构)

当你有自定义的HttpModule和HttpHandler时,需要同时在这两处添加

这个是为IIS6或者IIS7的经典模式用的

1

2

3

4

5

6

7

8

9

10

11

12

13

14

<system.web>

<httpHandlers>

<removeverb="*" path="*.asmx"/>

<addverb="*" path="CombineScriptHandler.aspx" validate="false" type="MvcScriptManager.CombineScriptHandler, MvcScriptManager, Version=1.0.0.0, Culture=neutral, PublicKeyToken=6eb4f344e8972dc6"/>

<addverb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

<addverb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

<addverb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/>

<addverb="*" path="*.mvc" validate="false" type="System.Web.Mvc.MvcHttpHandler, System.Web.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

</httpHandlers>

<httpModules>

<addname="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

<addname="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

</httpModules>

</system.web>

这个是为IIS7的集成模式用的

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

<system.webServer>

<validationvalidateIntegratedModeConfiguration="false"/>

<modulesrunAllManagedModulesForAllRequests="true">

<removename="ScriptModule"/>

<removename="UrlRoutingModule"/>

<addname="ScriptModule" preCondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

<addname="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

</modules>

<handlers>

<removename="WebServiceHandlerFactory-Integrated"/>

<removename="ScriptHandlerFactory"/>

<removename="ScriptHandlerFactoryAppServices"/>

<removename="ScriptResource"/>

<removename="MvcHttpHandler"/>

<removename="UrlRoutingHandler"/>

<addname="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

<addname="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

<addname="ScriptResource" preCondition="integratedMode" verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

<addname="MvcHttpHandler" preCondition="integratedMode" verb="*" path="*.mvc" type="System.Web.Mvc.MvcHttpHandler, System.Web.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

<addname="UrlRoutingHandler" preCondition="integratedMode" verb="*" path="UrlRouting.axd" type="System.Web.HttpForbiddenHandler, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>

</handlers>

</system.webServer>

在Web.config中配置handler节点时发现用vs2010和用vs2015竟然不一样,经过多次测试发现了一些倪端:

<configuration>

<!--vs2010中需要配这个,vs2015中可省开始-->

<system.web>

<compilation debug="true" targetFramework="4.0" />

<httpHandlers>

<add path="user.ashx(ajax中url请求的路径)" verb="POST,GET" type="MyHandler.UsersHander(方法的真实路径即:MyHandler类库下的UsersHander类)"/>

</httpHandlers>

</system.web>

<!--vs2010中需要配这个,vs2015中可省结束-->

<!--vs2015中需要配这个,vs2010中可省开始-->

<system.webServer>

<validation validateIntegratedModeConfiguration="false" /><!--没有上面内容时此处可省-->

<handlers>

<add path="user.ashx(ajax中url请求的路径)" verb="POST,GET" type="MyHandler.UsersHander(方法的真实路径即:MyHandler类库下的UsersHander类)"/>

</handlers>

</system.webServer>

<!--vs2015中需要配这个,vs2010中可省结束-->

</configuration>

用于健康检测:

namespace HealthCheck.Utils
{
    public class HealthCheckHandler : IHttpHandler
    {
        public bool IsReusable
        {
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Response.Write("ok");
        }
    }
}

  

<system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
    <handlers>
      <add name="HealthCheck" path="healthcheck.check" type="HealthCheck.Utils.HealthCheckHandler" verb="get"/>
    </handlers>
  </system.webServer>

  

将HealthCheck.Utils做成一个类库项目(需要继承IHttpHandler,引入相关引用),生成dll,项目中引入此dll,访问http://localhost:9152/HealthCheck.check 返回

Status Code:200 OK ok 来做健康检测