Monday 28 January 2013

Custom Errors in ASP.NET

What are Custom Errors?

There are certain errors/exceptions which are thrown by the operating system or the dot net framework when doing application programming we need to handle these errors properly. Dot net provides various ways to handle these kind of exceptions using CLR.
There are some errors which are application specific , there are also some errors which may pop-up in your application in adverse scenarios. When these kind of errors/exceptions are not handled properly ASP.NET shows a default error page that gives a brief description of the error along with the line number on which the error occurred. This page reveals a lot of information to the public , which may be dangerous when the information gets into wrong hands. Although these error pages are useful to a developer , who would wish to view this default error page, during the testing of the application since the description helps him in rectifying the error. But you would never want a user trying to access his application, to view this error page. The user would be least bothered to know about the error. Instead of showing the default error page, you should show a customized error page that would let the user send notification of the error to the administrator.
  
There are three different ways you can handle errors in ASP.Net.
Following are the error modes in which an ASP.NET application can work:
1) Off Mode
 2) On Mode
 3) RemoteOnly Mode

 By default, the mode value is set to "RemoteOnly".

Off Mode :When the error attribute is set to "Off", ASP.NET uses its default error page for both local and remote users in case of an error.

On Mode :In case of "On" Mode, ASP.NET uses user-defined custom error page instead of its default error page for both local and remote users. If a custom error page is not specified, ASP.NET shows the error page describing how to enable remote viewing of errors.

RemoteOnly :ASP.NET error page is shown only to local users. Remote requests will first check the configuration settings for the custom error page or finally show an IIS error.

You need to customize error page implementatio in configuration file by adding a value for an attribute defaultRedirect in the <customErrors> tag of the configuration file web.config.

This file determines configuration settings for the underlying application.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.web>
         <customErrors mode="Off" />
    </system.web>
</configuration>

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.web>
         <customErrors defaultRedirect="error.htm" mode="On" />
    </system.web>
</configuration>
RemoteOnly Mode
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.web>
         <customErrors defaultRedirect="error.htm" mode="RemoteOnly" />
    </system.web>
</configuration>

In the next article we will see how we can send errors to administrators.

No comments:

Post a Comment