Microsoft 網際網路資訊服務 (IIS) 版本 5.0 介紹能夠建立經由為內建的 ASP 伺服器物件的新方法的額外的自訂動態伺服器網頁 (ASP) 錯誤頁面稱為 Server.GetLastError() 傳回新的 ASPError 物件。
發生錯誤時編譯或執行 ASP 頁面時,IIS 會重新產生 500; 100 的錯誤,並執行 Server.Transfer() 方法,若要將控制項傳遞至目前定義的自訂錯誤網頁。(依預設此頁面是 /iishelp/common/500-100.asp)。如需有關 Server.Transfer() 方法的詳細資訊,請參閱下列的 「 Microsoft 知識庫 」 中的文件:
219294?
(http://support.microsoft.com/kb/219294/EN-US/
)
如何使用 Server.Transfer 方法
當控制項傳遞至自訂錯誤網頁時,Server.GetLastError() 方法可用來取得關於所發生的錯誤的詳細的資訊。Server.GetLastError() 方法會傳回具有下表列出的屬性的 ASPError 物件。(也可 IIS 5.0 線上文件中找到此資料表)。
摺疊此表格展開此表格
| 屬性 | 描述 |
|---|
| ASPCode | 傳回由 IIS 所產生的錯誤代碼。 |
| 數字 | 傳回標準 COM 錯誤碼。 |
| 來源 | 表示如果 ASP、 指令碼語言或物件的內部錯誤的來源。 |
| 檔案 | 指出錯誤發生時處理的.asp 檔案的名稱。 |
| 線條 | 指出在.asp 檔中產生錯誤的行。 |
| 描述 | 傳回錯誤的簡短描述。 |
| ASPDescription | 如果它是相關的 ASP 錯誤,請傳回錯誤的更詳細的描述。 |
下列步驟將會引導您完成設定自訂的 ASP 錯誤頁面。
- 將儲存下列 ASP 程式碼在指令碼資料夾作為 My500.asp"(不含引號):
<%@Language="VBSCRIPT"%>
<%
Option Explicit
On Error Resume Next
Response.Clear
Dim objError
Set objError = Server.GetLastError()
%>
<html>
<head>
<title>ASP 500 Error</title>
<style>
BODY { FONT-FAMILY: Arial; FONT-SIZE: 10pt;
BACKGROUND: #ffffff; COLOR: #000000;
MARGIN: 15px; }
H2 { FONT-SIZE: 16pt; COLOR: #ff0000; }
TABLE { BACKGROUND: #000000; PADDING: 5px; }
TH { BACKGROUND: #0000ff; COLOR: #ffffff; }
TR { BACKGROUND: #cccccc; COLOR: #000000; }
</style>
</head>
<body>
<h2 align="center">ASP 500 Error</h2>
<p align="center">An error occurred processing the page you requested.<br>
Please see the details below for more information.</p>
<div align="center"><center>
<table>
<% If Len(CStr(objError.ASPCode)) > 0 Then %>
<tr>
<th nowrap align="left" valign="top">IIS Error Number</th>
<td align="left" valign="top"><%=objError.ASPCode%></td>
</tr>
<% End If %>
<% If Len(CStr(objError.Number)) > 0 Then %>
<tr>
<th nowrap align="left" valign="top">COM Error Number</th>
<td align="left" valign="top"><%=objError.Number%>
<%=" (0x" & Hex(objError.Number) & ")"%></td>
</tr>
<% End If %>
<% If Len(CStr(objError.Source)) > 0 Then %>
<tr>
<th nowrap align="left" valign="top">Error Source</th>
<td align="left" valign="top"><%=objError.Source%></td>
</tr>
<% End If %>
<% If Len(CStr(objError.File)) > 0 Then %>
<tr>
<th nowrap align="left" valign="top">File Name</th>
<td align="left" valign="top"><%=objError.File%></td>
</tr>
<% End If %>
<% If Len(CStr(objError.Line)) > 0 Then %>
<tr>
<th nowrap align="left" valign="top">Line Number</th>
<td align="left" valign="top"><%=objError.Line%></td>
</tr>
<% End If %>
<% If Len(CStr(objError.Description)) > 0 Then %>
<tr>
<th nowrap align="left" valign="top">Brief Description</th>
<td align="left" valign="top"><%=objError.Description%></td>
</tr>
<% End If %>
<% If Len(CStr(objError.ASPDescription)) > 0 Then %>
<tr>
<th nowrap align="left" valign="top">Full Description</th>
<td align="left" valign="top"><%=objError.ASPDescription%></td>
</tr>
<% End If %>
</table>
</center></div>
</body>
</html>
- 設定自訂的 ASP 錯誤網頁:
- 開啟 [MMC 中的 [網際網路服務管理員]。
- 展開 [預設的網站]。
- 在 [指令碼] 資料夾上按一下滑鼠右鍵,然後選取 [內容]。
- 按一下 [自訂錯誤] 索引標籤。
- 向下捲動,並反白顯示 500; 100 HTTP 錯誤,然後按一下 [編輯內容。
- 確定 訊息類型 」 設定為 URL。
- 變更 URL 以"/ scripts/my500.asp"(不含引號)。
- 直到返回 [MMC,請按一下 [確定]。
- 測試新的錯誤網頁:
- 將所有下列頁面儲存在您的 [指令碼] 資料夾:
- 將下列頁面儲存為 Badpage1.asp 」 上 (不含引號):
<%@Language="VBSCRIPT"%>
<html>
<head>
<title>Bad Page 1</title>
</head>
<body>
<% Response.Write 1/0 %>
</body>
</html>
- 將下列頁面儲存為 Badpage2.asp"(不含引號):
<%@Language="VBSCRIPT"%>
<html>
<head>
<title>Bad Page 2</title>
</head>
<body>
<% Response.BadMethod "Hello" %>
</body>
</html>
- 下列網頁存 Badpage3.asp"(不含引號):
<%@Language="VBSCRIPT"%>
<html>
<head>
<title>Bad Page 3</title>
</head>
<body>
<%
Dim objBad
Set objBad = Server.CreateObject("BAD.OBJECT.CLASS")
%>
</body>
</html>
- 當您瀏覽的任何上述網頁時,您現在應該可以看到自訂錯誤網頁傳回瀏覽器。
注意:使用 [網際網路總管 5.0 版和以後若要檢視自訂 ASP 錯誤頁面時可能會傳回非預期的結果。 IE5 引入功能來取代 HTML 範本 HTTP 500 單位,數個其他通常傳回具有標準化、 非特定訊息的狀態代碼。 略過這項功能,並顯示狀態程式碼傳回給瀏覽器的確切文字,開啟 Internet Explorer,並巡覽至:
Tools | Internet Options | Advanced
然後取消核取標示為核取方塊
Show friendly HTTP error messages
額外的資訊,這項功能
218155?
(http://support.microsoft.com/kb/218155/EN-US/
)
「 超文字傳輸通訊協定錯誤訊息的描述