Wednesday, May 13, 2009

Custom error in web.config

Sometimes in asp.net , when we run an application , an error occurs. This error shows a default page with error type and its description.This type of page should never be displayed especially to the users as they want an application to be working perfectly allright and as user friendly as possible . To avoid this, we can provide with a customized web page with a message to the user.

To show this page we only need to create a page with a message and add a line of code in our web.config file. Follow these steps and you are done.

1. Create your error message Page with any message like "Sorry for your inconvenience but the page you are trying to access is not available at the moment".

2. Go to web.config and look for "system.web" tag

3. Add the "customErrors" tag between its opening and closing tags and add following attributes for it:

customErrors="On/Off/RemoteOnly" defaultRediret="ErrorPage.aspx"

In case mode is set Off , there is no need to provide with defaultRedirect attribute.

The three modes are described as follows :

1. Off Mode : In this case , whenever an error occurs, the default Error page of ASP.Net is shown both to the remote user and the local user. This is the mode which shows the complete error and by default , this error mode is used.

2. On Mode : In this case, whenever an error occurs , the customized error page we have created is displayed both to the local user and the remote user. In this case , if we do not specify the error page to be used , error page shows how we can enable Remote Mode to view the error.

3. Remote Only : In this case , whenever an error occurs , our customized error page with our Message is displayed to the remote user and default error page is shown to the remote user. This mode is the best as it hides error message from the user and shows it only to the concerned user.

defaultRedirect="ErrorPage.aspx" attribute specifies the page where the user is to be redirected in case of any error . This attribute is added only in case Mode is set to either On or RemoteOnly

No comments:

Post a Comment