Custom Errors on IIS 7.0 and Classic ASP.
In previous versions of IIS, there was a built-in error page called 500-100.asp. In IIS 7.0, this custom error page is not automatically listed. Here is a quick demo how I setup a couple ways to capture errors with custom errors. I tried this both with a Classic and Integrated application pools. Here is an article I wrote that I used in previous versions of IIS capturing errors.
Option 1)
Sample ASP page that has an divide by zero error. Lets call this webpage MyASPError.asp. It has On Error Resume Next, it’ll force the webpage execution to our error trapping.
<%
On Error Resume Next
x=1/0
If err.number > 0 Then
Response.Redirect “/500.asp?errDesc=” & err.description & “&errNum=” & err.number
End If
%>
<html>
<body>
<b>www.iislogs.com</b>
</body>
</html>
2) The 500.asp error page that
<html>
<body>
<%
response.write “<b>Error Description:</b>” & Request.Querystring(“errDesc”) & ” “
response.write “<b>Error Number:</b>” & Request.Querystring(“errNum”)
%>
<TABLE BORDER=”1″>
<TR>
<TD><B>Server Variable</B></TD><TD><B>Value</B></TD>
</TR>
<% For Each strKey In Request.ServerVariables %>
<TR>
<TD> <%= strKey %> </TD><TD> <%= Request.ServerVariables(strKey) %> </TD>
</TR>
<% Next %>
</TABLE>
</body>
Option 2:
Create a webpage called 500-100.asp and add 500.100.asp custom error page setting. You would create this in IIS Manager. (Start > run > inetmgr > site name > IIS section > Error Pages > Add action.
This seemed to work with some limiting factors. It wasn’t as flexible option1 . My 500-100.asp is the same as 500.asp, it writes out the server variables. The 500-100.asp displays the error, but does not change the URL, which is handy. I wasn’t able to capture the error like I did in option 1. If I find an alternative method, I’ll update the post.
Here is the 500-100.asp from XP
<%@ language=”VBScript” %>
<%
Option Explicit
Const lngMaxFormBytes = 200
Dim objASPError, blnErrorWritten, strServername, strServerIP, strRemoteIP
Dim strMethod, lngPos, datNow, strQueryString, strURL
If Response.Buffer Then
Response.Clear
Response.Status = “500 Internal Server Error”
Response.ContentType = “text/html”
Response.Expires = 0
End If
Set objASPError = Server.GetLastError
%>
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 3.2 Final//EN”>
<html dir=ltr>
<head>
<style>
a:link {font:8pt/11pt verdana; color:FF0000}
a:visited {font:8pt/11pt verdana; color:#4e4e4e}
</style>
<META NAME=”ROBOTS” CONTENT=”NOINDEX”>
<title>The page cannot be displayed</title>
<META HTTP-EQUIV=”Content-Type” Content=”text-html; charset=Windows-1252″>
</head>
<script>
function Homepage(){
<!-
// in real bits, urls get returned to our script like this:
// res://shdocvw.dll/http_404.htm#http://www.DocURL.com/bar.htm
//For testing use DocURL = “res://shdocvw.dll/http_404.htm#https://www.microsoft.com/bar.htm”
DocURL=document.URL;
//this is where the http or https will be, as found by searching for :// but skipping the res://
protocolIndex=DocURL.indexOf(“://”,4);
//this finds the ending slash for the domain server
serverIndex=DocURL.indexOf(“/”,protocolIndex + 3);
//for the href, we need a valid URL to the domain. We search for the # symbol to find the begining
//of the true URL, and add 1 to skip it – this is the BeginURL value. We use serverIndex as the end marker.
//urlresult=DocURL.substring(protocolIndex – 4,serverIndex);
BeginURL=DocURL.indexOf(“#”,1) + 1;
urlresult=DocURL.substring(BeginURL,serverIndex);
//for display, we need to skip after http://, and go to the next slash
displayresult=DocURL.substring(protocolIndex + 3 ,serverIndex);
InsertElementAnchor(urlresult, displayresult);
}
function HtmlEncode(text)
{
return text.replace(/&/g, ‘&’).replace(/’/g, ‘"’).replace(/</g, ‘<’).replace(/>/g, ‘>’);
}
function TagAttrib(name, value)
{
return ‘ ‘+name+’=”‘+HtmlEncode(value)+’”‘;
}
function PrintTag(tagName, needCloseTag, attrib, inner){
document.write( ‘<‘ + tagName + attrib + ‘>’ + HtmlEncode(inner) );
if (needCloseTag) document.write( ‘</’ + tagName +’>’ );
}
function URI(href)
{
IEVer = window.navigator.appVersion;
IEVer = IEVer.substr( IEVer.indexOf(‘MSIE’) + 5, 3 );
return (IEVer.charAt(1)==’.’ && IEVer >= ‘5.5’) ?
encodeURI(href) :
escape(href).replace(/%3A/g, ‘:’).replace(/%3B/g, ‘;’);
}
function InsertElementAnchor(href, text)
{
PrintTag(‘A’, true, TagAttrib(‘HREF’, URI(href)), text);
}
//->
</script>
<body bgcolor=”FFFFFF”>
<table width=”410″ cellpadding=”3″ cellspacing=”5″>
<tr>
<td align=”left” valign=”middle” width=”360″>
<h1 style=”COLOR:000000; FONT: 13pt/15pt verdana”><!-Problem->The page cannot be displayed</h1>
</td>
</tr>
<tr>
<td width=”400″ colspan=”2″>
<font style=”COLOR:000000; FONT: 8pt/11pt verdana”>There is a problem with the page you are trying to reach and it
cannot be displayed.</font></td>
</tr>
<tr>
<td width=”400″ colspan=”2″>
<font style=”COLOR:000000; FONT: 8pt/11pt verdana”>
<hr color=”#C0C0C0″ noshade>
<p>Please try the following:</p>
<ul>
<li id=”instructionsText1″>Click the
<a href=”javascript:location.reload()”>
Refresh</a> button, or try again later.<br>
</li>
<li>Open the
<script>
<!-
if (!((window.navigator.userAgent.indexOf(“MSIE”) > 0) && (window.navigator.appVersion.charAt(0) == “2”)))
{
Homepage();
}
//->
</script>
home page, and then look for links to the information you want. </li>
</ul>
<h2 style=”font:8pt/11pt verdana; color:000000″>HTTP 500.100 – Internal Server
Error – ASP error<br>
Internet Information Services</h2>
<hr color=”#C0C0C0″ noshade>
<p>Technical Information (for support personnel)</p>
<ul>
<li>Error Type:<br>
<%
Dim bakCodepage
on error resume next
bakCodepage = Session.Codepage
Session.Codepage = 1252
on error goto 0
Response.Write Server.HTMLEncode(objASPError.Category)
If objASPError.ASPCode > “” Then Response.Write Server.HTMLEncode(“, ” & objASPError.ASPCode)
Response.Write Server.HTMLEncode(” (0x” & Hex(objASPError.Number) & “)” ) & “<br>”
If objASPError.ASPDescription > “” Then
Response.Write Server.HTMLEncode(objASPError.ASPDescription) & “<br>”
elseIf (objASPError.Description > “”) Then
Response.Write Server.HTMLEncode(objASPError.Description) & “<br>”
end if
blnErrorWritten = False
‘ Only show the Source if it is available and the request is from the same machine as IIS
If objASPError.Source > “” Then
strServername = LCase(Request.ServerVariables(“SERVER_NAME”))
strServerIP = Request.ServerVariables(“LOCAL_ADDR”)
strRemoteIP = Request.ServerVariables(“REMOTE_ADDR”)
If (strServername = “localhost” Or strServerIP = strRemoteIP) And objASPError.File <> “?” Then
Response.Write Server.HTMLEncode(objASPError.File)
If objASPError.Line > 0 Then Response.Write “, line ” & objASPError.Line
If objASPError.Column > 0 Then Response.Write “, column ” & objASPError.Column
Response.Write “<br>”
Response.Write “<font style=””COLOR:000000; FONT: 8pt/11pt courier new””><b>”
Response.Write Server.HTMLEncode(objASPError.Source) & “<br>”
If objASPError.Column > 0 Then Response.Write String((objASPError.Column – 1), “-“) & “^<br>”
Response.Write “</b></font>”
blnErrorWritten = True
End If
End If
If Not blnErrorWritten And objASPError.File <> “?” Then
Response.Write “<b>” & Server.HTMLEncode( objASPError.File)
If objASPError.Line > 0 Then Response.Write Server.HTMLEncode(“, line ” & objASPError.Line)
If objASPError.Column > 0 Then Response.Write “, column ” & objASPError.Column
Response.Write “</b><br>”
End If
%>
</li>
<p>
<li>Browser Type:<br>
<%= Server.HTMLEncode(Request.ServerVariables(“HTTP_USER_AGENT”)) %>
</li>
<p>
<li>Page:<br>
<%
strMethod = Request.ServerVariables(“REQUEST_METHOD”)
Response.Write strMethod & ” “
If strMethod = “POST” Then
Response.Write Request.TotalBytes & ” bytes to “
End If
Response.Write Request.ServerVariables(“SCRIPT_NAME”)
lngPos = InStr(Request.QueryString, “|”)
If lngPos > 1 Then
Response.Write “?” & Server.HTMLEncode(Left(Request.QueryString, (lngPos – 1)))
End If
Response.Write “</li>”
If strMethod = “POST” Then
Response.Write “<p><li>POST Data:<br>”
If Request.TotalBytes > lngMaxFormBytes Then
Response.Write Server.HTMLEncode(Left(Request.Form, lngMaxFormBytes)) & ” . . .”
Else
Response.Write Server.HTMLEncode(Request.Form)
End If
Response.Write “</li>”
End If
%>
<p>
<li>Time:<br>
<%
datNow = Now()
Response.Write Server.HTMLEncode(FormatDateTime(datNow, 1) & “, ” & FormatDateTime(datNow, 3))
on error resume next
Session.Codepage = bakCodepage
on error goto 0
%>
</li>
</p>
<p>
<li>More information:<br>
<% strQueryString = “prd=iis&sbp=&pver=5.0&ID=500;100&cat=” & Server.URLEncode(objASPError.Category) & _
“&os=&over=&hrd=&Opt1=” & Server.URLEncode(objASPError.ASPCode) & “&Opt2=” & Server.URLEncode(objASPError.Number) & _
“&Opt3=” & Server.URLEncode(objASPError.Description)
strURL = “http://www.microsoft.com/ContentRedirect.asp?” & _
strQueryString
%>
<a href=”<%= strURL %>”>Microsoft Support</a>
</li>
</p>
</font></td>
</tr>
</table>
</body>
</html>
Before you implement this in a production site. I would thoroughly test any changes.
Hope this helps.
Steve Schofield
Microsoft MVP – IIS
Thanks for the hint!
Thanks for this guid.