Creating Custom ASP Error Pages

2013 年 9 月 13 日3140

|

Microsoft Internet Information Services (IIS) version 5.0 introduces the ability to create custom Active Server Pages (ASP) error pages through the addition of a new method for the built-in ASP Server object called Server.GetLastError() that returns a new ASPError object.

When an error occurs while you compile or run an ASP page, IIS generates a 500;100 error and executes a Server.Transfer() method to pass control to the currently defined custom error page. (By default this page is /iishelp/common/500-100.asp.) For more information on the Server.Transfer() method, see the following article in the Microsoft Knowledge Base:

219294

(http://http://www.zjjv.com///kb/219294/EN-US/

)

How to Use the Server.Transfer Method

When control is passed to the custom error page, the Server.GetLastError() method can be used to obtain detailed information regarding the error that occurred. The Server.GetLastError() method returns an ASPError object that has the properties listed in the following table. (This table can also be found in the IIS 5.0 online documentation.)

Collapse this tableExpand this table

Property Description

ASPCodeReturns an error code generated by IIS.NumberReturns the standard COM error code.SourceIndicates if the source of the error was internal to ASP, the scripting language, or an object.FileIndicates the name of the .asp file that was being processed when the error occurred.LineIndicates the line within the .asp file that generated the error.DescriptionReturns a short description of the error.ASPDescriptionReturns a more detailed description of the error if it is an ASP-related error.

The steps listed below will walk you through setting up a custom ASP error page.

    Save the following ASP code in your Scripts folder as "My500.asp" (without the quotation marks):





    <%@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>ASP 500 Error</h2>







    <p>An error occurred processing the page you requested.<br>



    Please see the details below for more information.</p>







    <div><center>







    <table>



    <% If Len(CStr(objError.ASPCode)) > 0 Then %>



    <tr>



    <th nowrap valign="top">IIS Error Number</th>



    <td valign="top"><%=objError.ASPCode%></td>



    </tr>



    <% End If %>



    <% If Len(CStr(objError.Number)) > 0 Then %>



    <tr>



    <th nowrap valign="top">COM Error Number</th>



    <td valign="top"><%=objError.Number%>



    <%=" (0x" & Hex(objError.Number) & ")"%></td>



    </tr>



    <% End If %>



    <% If Len(CStr(objError.Source)) > 0 Then %>



    <tr>



    <th nowrap valign="top">Error Source</th>



    <td valign="top"><%=objError.Source%></td>



    </tr>



    <% End If %>



    <% If Len(CStr(objError.File)) > 0 Then %>



    <tr>



    <th nowrap valign="top">File Name</th>



    <td valign="top"><%=objError.File%></td>



    </tr>



    <% End If %>



    <% If Len(CStr(objError.Line)) > 0 Then %>



    <tr>



    <th nowrap valign="top">Line Number</th>



    <td valign="top"><%=objError.Line%></td>



    </tr>



    <% End If %>



    <% If Len(CStr(objError.Description)) > 0 Then %>



    <tr>



    <th nowrap valign="top">Brief Description</th>



    <td valign="top"><%=objError.Description%></td>



    </tr>



    <% End If %>



    <% If Len(CStr(objError.ASPDescription)) > 0 Then %>



    <tr>



    <th nowrap valign="top">Full Description</th>



    <td valign="top"><%=objError.ASPDescription%></td>



    </tr>



    <% End If %>



    </table>







    </center></div>







    </body>



    </html>



Test the new error page:

    Save all of the following pages in your Scripts folder:

    Save the following page as "Badpage1.asp" (without the quotation marks):





    <%@Language="VBSCRIPT"%>



    <html>



    <head>



    <title>Bad Page 1</title>



    </head>



    <body>



    <% Response.Write 1/0 %>



    </body>



    </html>



    Save the following page as Badpage2.asp" (without the quotation marks):





    <%@Language="VBSCRIPT"%>



    <html>



    <head>



    <title>Bad Page 2</title>



    </head>



    <body>



    <% Response.BadMethod "Hello" %>



    </body>



    </html>



    Save the following page as "Badpage3.asp" (without the quotation marks):





    <%@Language="VBSCRIPT"%>



    <html>



    <head>



    <title>Bad Page 3</title>



    </head>



    <body>



    <%



    Dim objBad



    Set objBad = Server.CreateObject("BAD.OBJECT.CLASS")



    %>



    </body>



    </html>



    When you browse any of the above pages, you should now see the custom error page returned to the browser.

NOTE: When using Internet Explorer 5.0 and later to view Custom ASP Error Pages, unexpected results might be returned. IE5 introduced a feature to replace the HTML template for HTTP 500, and several other commonly returned status codes, with standardized, non-specific messages. To bypass this feature and show the exact text of the status code returned to the browser, open Internet Explorer and navigate to:





Tools | Internet Options | Advanced



then uncheck the checkbox labeled





Show friendly HTTP error messages



Additional information on this feature is available in

218155

(http://http://www.zjjv.com///kb/218155/EN-US/

)

Description of Hypertext Transport Protocol Error Messages

Article ID: 224070 - Last Review: November 21, 2006 - Revision: 1.2

APPLIES TO

Microsoft Internet Information Services 5.0

Keywords:

kbaspobj kbcodesnippet kbinfo KB224070

Retired KB Content Disclaimer

This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.

|

0 0