The customErrors section of the web.config file allows us to
handle individual errors differently. Errors are distinguished by their HTTP
error codes; we can also specify a particular page to redirect to that is based
on the HTTP error code that is raised.
The followings are some of the most common errors.
400: Bad Request - This means the request could not be
understood by the server. The
request
was denied due to a syntax error in the request.
401: Unauthorized - This means the request requires
authentication.
402: Payment required - This means the data is not
accessible at the time. The owner of the
space has not yet paid his or her service provider or
the server was unable to serve the data
that was requested.
403: Forbidden - This means the IP address or the
username/password entered were not correct
and the request was denied as there was no permission
to access the data.
404: Not found - This means the document requested either no
longer exists or has
never existed on the server.
405: Method not allowed- Means the method you are using to
access the document is not
allowed.
408: Request Timeout - Means the server has closed the
socket due to communications
between the client and server taking too long.
414: The URL requested is too long.
500: Internal server error. The server encountered an error
- This is most often caused by a
scripting problem, a failed database access attempt,
or similar reasons.
503: Service unavailable- This means the server is
overloaded or down for maintenance and was
unable to process the client request.
A complete HTTP status code listing is available online. We
may expand the customErrors section of the web.config file to handle specific
error codes. We can achieve the expansion by adding error elements under the
customErrors element. The following sample shows how we may accomplish this.
Listing 10
<customErrors mode="RemoteOnly" defaultRedirect="CustomErrorPage.aspx">
<error statusCode="400" redirect="ErrorPage400.aspx" />
<error statusCode="401" redirect="ErrorPage401.aspx" />
<error statusCode="402" redirect="ErrorPage402.aspx" />
<error statusCode="403" redirect="ErrorPage403.aspx" />
<error statusCode="404" redirect="ErrorPage404.aspx" />
<error statusCode="408" redirect="ErrorPage408.aspx" />
<error statusCode="414" redirect="ErrorPage414.aspx" />
<error statusCode="500" redirect="ErrorPage500.aspx" />
<error statusCode="503" redirect="ErrorPage503.aspx" />
</customErrors>
The above sample handles each specific error with the
corresponding page. The statusCode attribute of the error element designates
the error and the redirect attribute defines the page to use. This allows us
to customize the error message based upon the error encountered. The default
error page handles any error that is not specified.