共用方式為


新增 HTTP 錯誤 <>

概觀

<error>集合的 <httpErrors> 元素會將唯一的錯誤對應新增至 HTTP 錯誤集合。

相容性

版本 備註
IIS 10.0 <error> IIS 10.0 中未修改專案。
IIS 8.5 <error> 在 IIS 8.5 中修改專案。
IIS 8.0 在 IIS 8.0 中未修改專案 <error>
IIS 7.5 <error> 在 IIS 7.5 中修改專案。
IIS 7.0 <error>集合的 <httpErrors> 元素是在 IIS 7.0 中引進的。
IIS 6.0 <httpErrors>集合會取代IIsWebService中繼基底物件的 IIS 6.0 HttpErrors區段。

安裝程式

<error>集合的 <httpErrors> 元素包含在 IIS 7 的預設安裝中。

作法

如何新增自訂錯誤頁面

  1. (IIS) 管理員開啟 Internet Information Services

    • 如果您使用 Windows Server 2012 或 Windows Server 2012 R2:

      • 在工作列上,依序按一下 [伺服器管理員]、[工具],然後按一下 [Internet Information Services] ([IIS) 管理員]。
    • 如果您使用 Windows 8 或 Windows 8.1:

      • 按住Windows鍵,按字母X,然後按一下[主控台]。
      • 按一下 [ 系統管理工具],然後按兩下 [Internet Information Services] ([IIS) 管理員]。
    • 如果您使用 Windows Server 2008 或 Windows Server 2008 R2:

      • 在工作列上,按一下 [ 開始],指向 [ 系統管理工具],然後按一下 [ Internet Information Services (IIS) 管理員]。
    • 如果您使用 Windows Vista 或 Windows 7:

      • 在工作列上,按一下 [開始],然後按一下[主控台]。
      • 按兩下 [ 系統管理工具],然後按兩下 [Internet Information Services] ([IIS) 管理員]。
  2. 在 [ 連線 ] 窗格中,展開伺服器名稱、[ 網站],然後流覽至您要設定自訂錯誤頁面的網站或應用程式。

  3. 在 [ 首頁] 窗格中,按兩下 [錯誤頁面]。
    [首頁] 窗格的螢幕擷取畫面,其中已醒目提示 [錯誤頁面] 選項。

  4. 在 [ 動作] 窗格中,按一下 [ 新增...

  5. 在 [ 新增自訂錯誤頁面] 對話方塊的 [ 狀態碼] 底下,輸入您要建立自訂錯誤訊息的 HTTP 狀態碼數目。
    [新增自訂錯誤頁面] 對話方塊的螢幕擷取畫面,其中顯示 [狀態碼] 欄位。

  6. 在 [ 回應動作] 區段中,執行下列其中一項動作:

    • 針對自訂錯誤,選取 [將靜態檔案的內容插入錯誤回應 中,以提供靜態內容,例如.html檔案。
    • 選取 [在此網站上執行 URL ] 來提供動態內容,例如自訂錯誤的 .asp 檔案。
    • 選取 [使用 302 重新導向回應 ],將用戶端瀏覽器重新導向至包含自訂錯誤檔案的不同 URL。
  7. 在 [ 檔案路徑 ] 文字方塊中,如果您選擇 [ 將靜態檔案的內容插入至錯誤回應 ] 或自訂錯誤頁面的 URL,請輸入自訂錯誤頁面的路徑,如果您使用 此網站上的 [執行 URL ] 或 [ 回應 302 重新導向],然後按一下 [ 確定]。

    注意

    如果您選取 [在此網站上執行 URL],則路徑必須是相對路徑。 如果您選取 [使用 302 重新導向回應],則 URL 必須是絕對 URL。

    注意

    無法自訂下列 HTTP 錯誤:400、403.9、411、414、500、500.11、500.14、500.15、501、503 和 505。

組態

屬性

屬性 描述
path 必要的字串屬性。

指定回應 statusCodesubStatusCode 屬性所指定之 HTTP 錯誤的檔案路徑或 URL。 如果您選擇 [檔案回應模式],您可以指定自訂錯誤頁面的路徑。 如果您選擇 ExecuteURL 回應模式,路徑必須是伺服器相對 URL (,例如 /404.htm) 。 如果您選擇 [ 重新 導向回應模式],則必須輸入絕對 URL (例如 www.contoso.com/404.htm ,) 。
prefixLanguageFilePath 選擇性字串屬性。

指定產生自訂錯誤路徑時的初始路徑區段。 此區段會出現在自訂錯誤路徑的語言特定部分之前。 例如,在路徑C:\Inetpub\Custerr\en-us\404.htm中,C:\Inetpub\Custerr 是 prefixLanguageFilePath
responseMode 選擇性列舉屬性。

指定如何傳回自訂錯誤內容。

responseMode屬性可以是下列其中一個可能的值。 預設值為 File
描述
File 例如,提供靜態內容,例如自訂錯誤的.html檔案。

如果 responseMode 設定為 File,路徑屬性值必須是檔案路徑。

數值為 0
ExecuteURL 例如,提供動態內容 (,在路徑屬性中指定自訂錯誤的 .asp 檔案) 。

如果 responseMode 設定為 ExecuteURL,路徑值必須是伺服器相對 URL。

數值為 1
Redirect 將用戶端瀏覽器重新導向至路徑屬性中指定的 URL,其中包含自訂錯誤檔案。

如果 responseMode 設定為 [重新導向],路徑值必須是絕對 URL。

數值為 2
statusCode 必要的 uint 屬性。

指定您要為其建立自訂錯誤訊息的 HTTP 狀態碼數目。 可接受的值介於 400 到 999 的範圍內。
subStatusCode 選擇性 int 屬性。

指定要建立自訂錯誤訊息的 HTTP 子狀態碼數目。 可接受的值介於 -1 到 999 的範圍內。

預設值是 -1

子元素

無。

組態範例

下列組態範例,當包含在網站或應用程式的 Web.config 檔案中時,會使用 errorMode 屬性,只允許在本機電腦上顯示詳細的錯誤訊息。 它也會使用 defaultResponseMode 屬性來設定網站或應用程式的回應模式。 然後,此範例會移除 500 狀態碼的繼承錯誤訊息。 接下來,它會將 prefixLanguageFilePath 屬性設定為 IIS 應該搜尋新自訂錯誤頁面的目錄,並將 路徑 屬性設定為500.htm,其中包含自訂錯誤訊息的檔案。

<configuration>
   <system.webServer>
      <httpErrors errorMode="DetailedLocalOnly" defaultResponseMode="File" >
         <remove statusCode="500" />
         <error statusCode="500"
            prefixLanguageFilePath="C:\Contoso\Content\errors"
            path="500.htm" />
       </httpErrors>
   </system.webServer>
</configuration>

範例程式碼

下列範例會新增所有狀態碼 404 錯誤的新檔案,其子狀態為 5,IIS 會針對「URL 序列遭拒」錯誤傳回。 在這些範例中,前置詞路徑會設定為 「%SystemDrive%\inetpub\custerr」,並將檔案名指定為 「404.5.htm」。

AppCmd.exe

appcmd.exe set config -section:system.webServer/httpErrors /+"[statusCode='404',subStatusCode='5',prefixLanguageFilePath='%SystemDrive%\inetpub\custerr',path='404.5.htm']" /commit:apphost

注意

當您使用AppCmd.exe設定這些設定時,請務必將 認可 參數 apphost 設定為 。 這會將組態設定認可至ApplicationHost.config檔案中適當的位置區段。

C#

using System;
using System.Text;
using Microsoft.Web.Administration;

internal static class Sample
{
   private static void Main()
   {
      using (ServerManager serverManager = new ServerManager())
      {
         Configuration config = serverManager.GetApplicationHostConfiguration();
         ConfigurationSection httpErrorsSection = config.GetSection("system.webServer/httpErrors");
         ConfigurationElementCollection httpErrorsCollection = httpErrorsSection.GetCollection();

         ConfigurationElement errorElement = httpErrorsCollection.CreateElement("error");
         errorElement["statusCode"] = 404;
         errorElement["subStatusCode"] = 5;
         errorElement["prefixLanguageFilePath"] = @"%SystemDrive%\inetpub\custerr";
         errorElement["path"] = @"404.5.htm";
         httpErrorsCollection.Add(errorElement);

         serverManager.CommitChanges();
      }
   }
}

VB.NET

Imports System
Imports System.Text
Imports Microsoft.Web.Administration

Module Sample
   Sub Main()
      Dim serverManager As ServerManager = New ServerManager
      Dim config As Configuration = serverManager.GetApplicationHostConfiguration
      Dim httpErrorsSection As ConfigurationSection = config.GetSection("system.webServer/httpErrors")
      Dim httpErrorsCollection As ConfigurationElementCollection = httpErrorsSection.GetCollection

      Dim errorElement As ConfigurationElement = httpErrorsCollection.CreateElement("error")
      errorElement("statusCode") = 404
      errorElement("subStatusCode") = 5
      errorElement("prefixLanguageFilePath") = "%SystemDrive%\inetpub\custerr"
      errorElement("path") = "404.5.htm"
      httpErrorsCollection.Add(errorElement)

      serverManager.CommitChanges()
   End Sub
End Module

JavaScript

var adminManager = new ActiveXObject('Microsoft.ApplicationHost.WritableAdminManager');
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST";
var httpErrorsSection = adminManager.GetAdminSection("system.webServer/httpErrors", "MACHINE/WEBROOT/APPHOST");
var httpErrorsCollection = httpErrorsSection.Collection;

var errorElement = httpErrorsCollection.CreateNewElement("error");
errorElement.Properties.Item("statusCode").Value = 404;
errorElement.Properties.Item("subStatusCode").Value = 5;
errorElement.Properties.Item("prefixLanguageFilePath").Value = "%SystemDrive%\\inetpub\\custerr";
errorElement.Properties.Item("path").Value = "404.5.htm";
httpErrorsCollection.AddElement(errorElement);

adminManager.CommitChanges();

VBScript

Set adminManager = createObject("Microsoft.ApplicationHost.WritableAdminManager")
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST"
Set httpErrorsSection = adminManager.GetAdminSection("system.webServer/httpErrors", "MACHINE/WEBROOT/APPHOST")
Set httpErrorsCollection = httpErrorsSection.Collection

Set errorElement = httpErrorsCollection.CreateNewElement("error")
errorElement.Properties.Item("statusCode").Value = 404
errorElement.Properties.Item("subStatusCode").Value = 5
errorElement.Properties.Item("prefixLanguageFilePath").Value = "%SystemDrive%\inetpub\custerr"
errorElement.Properties.Item("path").Value = "404.5.htm"
httpErrorsCollection.AddElement errorElement

adminManager.CommitChanges()