Friday, November 14, 2008 Custom Error Handling from web.confing file’s customErrors section

In my previous posts, I have talked about the user friendly error handling in 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 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 web.config file. Here is the syntax:

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


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

redirect="~/Errors/ErrorPage401.aspx" />

redirect="~/Errors/ErrorPage402.aspx" />

redirect="Errors/ErrorPage403.aspx" />

redirect="Errors/ErrorPage404.aspx" />

redirect="Errors/ErrorPage408.aspx" />

redirect="Errors/ErrorPage414.aspx" />

redirect="Errors/ErrorPage500.aspx" />

redirect="Errors/ErrorPage503.aspx" />


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! Happy programming!!


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