Custom error in web.config


Sometimes in asp.net, when we run an application, an error occurs. This error shows  page with big yellow colored area with the error highlighted in red (a page that the developers never want to see on their site). This type of page should never be displayed to the end users. As a normal internet user, i would also feel bad if see something like this and keep wondering why the website is not working .

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

customError

Select a mode that suits your requirements and then run the application. Try to force an exception and see the results. Happy coding…!!!

Advertisements

About Jasminder

.Net developer and blogger by profession, keen to learn new technologies, love nature, music and cricket.
This entry was posted in .Net and tagged . Bookmark the permalink.

I have a suggestion..

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s