如何:為 IIS 7.0 配置 <system.webServer> 節2008-06-14 22:26http://technet.microsoft.com/zh-cn/sysinternals/bb763179.aspx
如何:為 IIS 7.0 配置 <system.webServer> 節
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 將提供一個默認文件。
配置默認文件
如果應用程序沒有 Web.config 文件,請使用 Visual Studio 或文本編輯器創建該文件。
有關更多信息,請參見編輯 ASP.NET 配置文件。
如果 Web.config 文件尚未包含 system.webServer 節,請在 configuration 元素中創建該節,如下面的示例所示:
復制代碼
<configuration>
<system.webServer>
</system.webServer>
</configuration>
在 system.webServer 元素內,創建一個 defaultDocument 元素。
在 defaultDocument 元素內,創建一個 files 元素。
在 files 元素內創建一個 add 元素,並在 value 屬性內指定默認文件的路徑和名稱。
下面的示例演示了一個 system.webServer 節,該節配置為提供 Products.aspx 文件作為默認文件。
復制代碼
<configuration>
<system.webServer>
<defaultDocument>
<files>
<add value="Products.aspx" />
</files>
</defaultDocument>
</system.webServer>
</configuration>
注冊托管代碼模塊每次請求時都會調用托管代碼模塊,通過該模塊可對請求或響應進行自定義。
配置自定義托管代碼模塊
如果應用程序沒有 Web.config 文件,請使用 Visual Studio 或文本編輯器創建該文件。
有關更多信息,請參見編輯 ASP.NET 配置文件。
如果 Web.config 文件尚未包含 system.webServer 節,請在 configuration 元素中創建該節,如下面的示例所示:
復制代碼
<configuration>
<system.webServer>
</system.webServer>
</configuration>
在 system.webServer 元素內,創建一個 modules 元素。
在 modules 元素內創建一個 add 元素,並在 name 和 type 屬性中指定自定義模塊。
實際的名稱和類型取決於要添加的模塊。下面的示例演示如何添加名為 CustomModule 的自定義模塊,該模塊將實現為類型 Samples.CustomModule。
復制代碼
<configuration>
<system.webServer>
<modules>
<add name="CustomModule" type="Samples.CustomModule" />
</modules>
</system.webServer>
</configuration>
向模塊注冊中添加 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(英國英語)。
配置自定義響應標頭
如果應用程序沒有 Web.config 文件,請使用 Visual Studio 或文本編輯器創建該文件。
有關更多信息,請參見編輯 ASP.NET 配置文件。
如果 Web.config 文件尚未包含 system.webServer 節,請在 configuration 元素中創建該節,如下面的示例所示:
復制代碼
<configuration>
<system.webServer>
</system.webServer>
</configuration>
在 system.webServer 元素內,創建一個 httpProtocol 元素。
在 httpProtocol 元素內,創建一個 customHeaders 元素。
在 customHeaders 元素內創建一個 add 標記,並在 name 和 value 屬性中指定自定義標頭。
實際的名稱和類型將取決於該標頭在應用程序中的功能。下面的示例演示如何添加名為 CustomHeader 且值為 CustomHeader 的自定義標頭。
復制代碼
<configuration>
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="CustomHeader" value="CustomHeader" />
<customHeaders>
</httpProtocol>
</system.webServer>
</configuration>
原文鏈接:https://blog.csdn.net/happymagic/article/details/6733232
