CODEDIGEST
Home » Articles
Search
 

Technologies
 

CodeDigest Navigation
 

Technology News
No News Feeds available at this time.
 

Community News
No News Feeds available at this time.
 
Custom Error Page in ASP.Net

By Satheesh Babu
Posted On Feb 24,2008
Article Rating:
Average Rating: 5
No of Ratings: 1
No of Comments: 0
Category: ASP.Net
Print this article.

Custom Error Page in ASP.Net

Whenever any error occurs during the execution of our ASP.net applications we normally show an error page. It is done to hide the technical error information to the user and show a generic error message. This article will help us to implement the custom error page for ASP.Net application. I will list some of the possible ways that we can use to show the error page to the user. We redirect the user to a custom error page by 3 ways.

  • Using Page_Error Event
  • Using Application_Error event in Global.asax
  • Using Web.Config

Using Page_Error Event:

Using this event we can seize the error at page level. Instead of writing this event at every page we can move to BasePage class like below.

 

public class BasePage : System.Web.UI.Page

{

    void Page_Error(object sender, EventArgs e)

    {

        Server.Transfer("Error.aspx");

    }

}

And then we can inherit every page with this object instead of Page object. So an aspx.cs file will look like,

public partial class _Default : BasePage

{

    protected void Page_Load(object sender, EventArgs e)

    {

     }

}

 

 With this approach we can prevent the redundant code in every page. If we want to handle error differently in different pages then we can write this event in everypage.

In error page we can get the actual exception or error information using Server.GetLastError() method like below,

 

public partial class Error : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

        Response.Write(Server.GetLastError());

    }

}

 

Most of the time it is not advisable to show the error information using Server.GetLastError() because the error message will be technical which is not appropriate to the enduser.

 

Using Application_Error event in Global.asax

We can trap the error in Application_Error if it is not trapped in Page_Error event. We can log the error information into a Eventlog,etc..

void Application_Error(object sender, EventArgs e)

{

//Event log code

Server.ClearError();

}

Using Web.Config

This is most commonly used approach using configuration settings.

<customErrors defaultRedirect="Error.aspx" mode="On">

</customErrors>

Note:

If we trapped the error in Application_Error event and cleared the error using Server.ClearError(), then <customError> section won’t work because the error is cleared. So we can log the error into eventlog in Application_Error event and then redirect to a generic error page using <customError> section. Now the enduser will see the generic error page and we can enough technical information in event log for fixing the error.

Similar Articles

Latest Articles

You can contribute to CodeDiget.Com:
Donate to CodeDigest.com
Article Feedback
Title  
Submitted By  
Comment  
Enter the verification number
 
Comments