為 IIS 7.0 配置 <system.webServer>

來源:互聯網
上載者:User

標籤:des   style   blog   http   io   ar   color   os   使用   

 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節中指定的所有Managed 程式碼模組和處理常式。與 IIS 的早期版本相同,Managed 程式碼模組和處理常式必須在 system.web 節的 httpModules 和 httpHandlers 元素中定義。

本主題闡釋需要修改 system.webServer 節的三個常見配置任務:

  • 添加預設檔案,以便在請求 URL 未包含特定的檔案時,提供該預設檔案。

  • 註冊Managed 程式碼模組。

  • 添加自訂響應標題。

配置預設檔案

當請求 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>
註冊Managed 程式碼模組 

每次請求時都會調用Managed 程式碼模組,通過該模組可對請求或響應進行自訂。

配置自訂Managed 程式碼模組
  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>

     

為 IIS 7.0 配置 <system.webServer>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.