29 June 2004

Customised application error page

We can specify error pages to intercept HTTP errors using Web.config file.
To display a specific error handling page for HTTP status code such like 404(NotFound), 500(InternalServerError), 401(Unauthorized)etc we can add the following element to the web app’s web.config file:

<customErrors mode="On" defaultRedirect="./Errors/wehaveaproblem.aspx" >
<error statusCode="401" redirect="login.aspx" />
<error statusCode="500" redirect="./Errors/wehaveaproblem.aspx " />
<!-- redirect to the 404 page only valid for URLs that end with aspx.
Need to config IIS to take other page types -->
<error statusCode="404" redirect="./Errors/404.aspx" />

To the 404 error, by default it only works for .aspx and .asp pages. Requesting pages like .htm or .html will still fail back to default MS 404 page. To enable our IIS App handling .htm, you need to map .htm/.html request so it could be handled by aspnet_isapi.dll.
Open IIS manager, click ‘(Application) configuration…’ and in ‘(‘Application mapping’ tab, add htm extension. Map it to ‘((install_drive) \WINNT\Microsoft.NET\Framework\(version)\aspnet_isapi.dll.

Once this is done, you may notice the app is giving back a 500 at default home page (http://www.mySite.com/), if you use a default.aspx page. This is because the default document retrieval order is set to default.htm-default.asp-iistart.asp-default.aspx. Add a dummy default.htm page to redirect the request to default.aspx as following(need client side to enable scripting):

<script language=javascript>

The other way to do display customised error page is by configuring IIS in Custom Errors Setting.

No comments: