user2179026
user2179026

Reputation:

How to add generic error page for all other run-time errors?

I am trying to tweak with

 <customErrors mode="On" defaultRedirect ="~/System/err.aspx">
 </customErrors>

in web.config file

So that whenever there is runtime error or any type error in any page, the error message should not be displayed on that page instead of this it should be redirected to err.aspx page.

Is there any such configuration.

Upvotes: 2

Views: 1640

Answers (3)

Jitendra Sawant
Jitendra Sawant

Reputation: 698

(1) The approach u are following is right. You need to specify different error pages for different types of errors (if you want to handle different error types in different ways)like mentioned by nadeem,

(2) 2nd approach preferable is using Application_Error in Global.asax like below:

void Application_Error(object sender, EventArgs e)
{
  // Code that runs when an unhandled error occurs

  // Get the exception object.
  Exception exc = Server.GetLastError();


  // For other kinds of errors give the user some information
  // but stay on the default page
  Response.Write("<h2>Global Page Error</h2>\n");
  Response.Write(
      "<p>" + exc.Message + "</p>\n");
  Response.Write("Return to the <a href='Default.aspx'>" +
      "Default Page</a>\n");
  // Clear the error from the server
  Server.ClearError();
}

(3) 3rd approach is use of Application_Error and custom error pages together. using Application_Error for getting exception details and passing it to the error page like,

protected void Application_Error(object sender, EventArgs e)
{
    Exception err = Server.GetLastError();
    Session.Add("LastError", err);
}

void Session_Start(object sender, EventArgs e) 
{      
    Session["LastError"] = ""; //initialize the session
}

In error page :

 protected void Page_Load(object sender, EventArgs e)
{
    Exception err = Session["LastError"] as Exception;
    //Exception err = Server.GetLastError();
    if (err != null)
    {
        err = err.GetBaseException();
        lblErrorMsg.Text = err.Message;
        lblSource.Text = err.Source;
        lblInnerEx.Text = (err.InnerException != null) ? err.InnerException.ToString() : "";
        lblStackTrace.Text = err.StackTrace;
        Session["LastError"] = null;
    }
}

Upvotes: 1

Darren S
Darren S

Reputation: 940

We prefer to catch these in Global.asax so that we can do some custom execption handling such as writing to event logs, saving to databases and email support staff, before we redirect to an error page.

In Global.asax.cs;

    void Application_Error(object sender, EventArgs e)
    {

        // Get the exception
        Exception exc = Server.GetLastError();

        // Do something with the exception, write it to the event log, save it to the db, email someone who cares etc

        // Clear the error
        Server.ClearError();

        // Redirect to a generic error page
        Response.Redirect("~/System/err.aspx");

    }

Futher usage examples here: https://msdn.microsoft.com/en-us/library/24395wz3(v=vs.140).aspx

Upvotes: 0

HEEN
HEEN

Reputation: 4721

There are many links available on the sites to get you out of this, Hence if you didn't get that see the below code. You need to add the code like below under

configuration section. Also there should be a page with name err with all the information which cleary says that it is an error page.

<configuration>
<system.web>
   <customErrors mode="On" defaultRedirect="err.aspx">
      <error statusCode="404" redirect="404.aspx" />
      <error statusCode="500" redirect="500.aspx" />
   </customErrors>
</system.web>

Also have a look at an Example here for clear understanding

Hope that helps

Upvotes: 1

Related Questions