Saturday, 3 October 2015

Handle Error Modes in ASP.NET redirec to proper page - Web.config

Leave a Comment
Most of the website and web application must have error handler. Some beginner might question what are the error handler should have in one website. Below are the basic error handler that you need to set in the website :
  1. Page Not Found - 404
  2. Internal server Error - 500
  3. Access Denied - 403
The above error page must have in one website. This is because when application catch error, the end users will see asp.net designed page without details error information that might not user friendly for the end users. To handle this errors, all you need to do are two things which is Create the above error page and set in Web.Config file in asp.net.

Below is a example of setting in web.config file.



Web.Config

 <configuration>  
 <system.web>  
  <customErrors defaultRedirect="~/500.html?" mode="RemoteOnly">  
    <error statusCode="404" redirect="~/404.html" />  
    <error statusCode="500" redirect="~/500.html" />  
    <error statusCode="403" redirect="~/AccessDenied.html" />  
   </customErrors>  
 </system.web>  
 </configuration>  

As you can notice, i set the default error to defaultRedirect="~/500.html?" . The symbol "?" is to avoid web application passed unwanted querystring when the web application  catch the error..

Modes Available List

Error ModeDescription
RemoteOnlyError page are shown for all end user except localhost user. Example if your website is accessible by domain abc.com, so everyone who access the website using abc.com will see the serve the error page. If you access the website using localhost / 127.0.0.1 - you will not see the error page
OffAll user will see default error page by asp.net with details error information such as line number error and some of your coding highlight by the compiler where the error catch.
OnNo matter local or remote user will see the error page created by you (404.html / 500.html or AccessDenied.html)







By
NOTE : – If You have Found this post Helpful, I will appreciate if you can Share it on Facebook, Twitter and Other Social Media Sites. Thanks =)

0 comments:

Post a Comment

Subscribe to our newsletter to get the latest updates to your inbox.

Your email address is safe with us!




Founder of developersnote.com, love programming and help others people. Work as Software Developer. Graduated from UiTM and continue study in Software Engineering at UTMSpace. Follow him on Twitter , or Facebook or .



Powered by Blogger.