Friday, November 14, 2008

Asp.net Custom Error Handling from web.confing file’s customErrors section

In my previous posts, I have talked about the user friendly error handling in asp.net web application. It was focused on practicing more user friendly and interacting error handling through proper error message in well-designed user interfaces. This means the post seeds some ideas on presentation layer error handling. Further I also posted some earlier the way to asp.net web application error handling in the sql server stored procedures. It was basically related to database level error handling. There exists some great way to another effective error handling. Let me show the scenario.

There may occur a variety of cases like access denied, file not available, some unknown error etc. It would be far better if we could automatically redirect to the error page relevant to the type of error and ask the clients for excuse! Clear? Yeah, we can define the custom errors and the pages to redirect to in the web.config file. Here is the list of errors.

Error status code 400: Bad request. The request cannot be server.
Error status code 401: No authority to view the page.
Error status code 402: Page not available at this time. Payment is required.
Error status code 403: The page is forbidden.
Error status code 404: Requested page not available. (Or it does not exist, Check the URL's syntax)
Error status code 408: The request times out
Error status code 414: The request URL too long to server
Error status code 500: Internal server error has occurred.
Error status code 503: Requested service currently unavailable.

You can now make the relevant pages and prepare to point them in the asp.net web.config file. Here is the syntax:

<!--Redirect to the corresponding error page when errors occur-->

<customErrors
defaultRedirect="~/Errors/CustomErrorPage.aspx"
RemoteOnly="true">

<error statusCode="400"
redirect="~/Errors/ErrorPage400.aspx" />

<error
statusCode="401"
redirect="~/Errors/ErrorPage401.aspx" />

<error
statusCode="402"
redirect="~/Errors/ErrorPage402.aspx" />

<error
statusCode="403"
redirect="Errors/ErrorPage403.aspx" />

<error
statusCode="404"
redirect="Errors/ErrorPage404.aspx" />

<error
statusCode="408"
redirect="Errors/ErrorPage408.aspx" />

<error
statusCode="414"
redirect="Errors/ErrorPage414.aspx" />

<error
statusCode="500"
redirect="Errors/ErrorPage500.aspx" />

<error
statusCode="503"
redirect="Errors/ErrorPage503.aspx" />

</customErrors>

Put the above code within <system.web> and <system.web> sections. The error pages will be visible if the request is remote. To make the page redirections work in local domain, set the RemoteOnly false. Easy, isn't it? And more sophisticated too. At least saves from coding it everytime in each page. Great asp.net! Happy programming!!

0 comments:

Post a Comment

Hope you liked this post. You can leave your message or you can put your valuable suggestions on this post here. Thanks for the sharing and cooperation!

Popular Posts

Recent Articles