如何:為 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 節的 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>
  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>
請參見

任務

概念

參考

 

當你有自定義的HttpModule和HttpHandler時,需要同時在這兩處添加

這個是為IIS6或者IIS7的經典模式用的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
< system.web >
< httpHandlers >
       < remove  verb="*" path="*.asmx"/>
       < add  verb="*" path="CombineScriptHandler.aspx" validate="false" type="MvcScriptManager.CombineScriptHandler, MvcScriptManager, Version=1.0.0.0, Culture=neutral, PublicKeyToken=6eb4f344e8972dc6"/>
       < add  verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
       < add  verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
       < add  verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/>
       < add  verb="*" path="*.mvc" validate="false" type="System.Web.Mvc.MvcHttpHandler, System.Web.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
     </ httpHandlers >
     < httpModules >
       < add  name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
       < add  name="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 >
     < validation  validateIntegratedModeConfiguration="false"/>
     < modules  runAllManagedModulesForAllRequests="true">
       < remove  name="ScriptModule"/>
       < remove  name="UrlRoutingModule"/>
       < add  name="ScriptModule" preCondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
       < add  name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
     </ modules >
     < handlers >
       < remove  name="WebServiceHandlerFactory-Integrated"/>
       < remove  name="ScriptHandlerFactory"/>
       < remove  name="ScriptHandlerFactoryAppServices"/>
       < remove  name="ScriptResource"/>
       < remove  name="MvcHttpHandler"/>
       < remove  name="UrlRoutingHandler"/>
       < add  name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
       < add  name="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"/>
       < add  name="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"/>
       < add  name="MvcHttpHandler" preCondition="integratedMode" verb="*" path="*.mvc" type="System.Web.Mvc.MvcHttpHandler, System.Web.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
       < add  name="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 來做健康檢測


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM