1

I have web application in asp.net. I have to implement custom error page. Means if any eror occurs(runtime). I have to display exception and stacktrace on errorpage.aspx shall i handle from master page or on page level and how.

<customErrors mode="On" defaultRedirect="~/Error_Page.aspx"></customErrors>
2
  • 1
    If you set custom errors to Off it'll just show you the error and stacktrace in the asp default exception page. Don't know if that'll do it for you? Commented Dec 12, 2012 at 9:58
  • i have to write details and stack trace about error on ErrorPage.aspx from users point of view Commented Dec 12, 2012 at 10:11

5 Answers 5

4

You can handle it in global.asax :

protected void Application_Error(object sender, EventArgs e)
{
   Exception ex = System.Web.HttpContext.Current.Error;
   //Use here
   System.Web.HttpContext.Current.ClearError();
   //Write custom error page in response
   System.Web.HttpContext.Current.Response.Write(customErrorPageContent);
   System.Web.HttpContext.Current.Response.StatusCode = 500;
}
Sign up to request clarification or add additional context in comments.

Comments

2

Please do not use Redirection as a means of displaying error messages, because it breaks HTTP. In case of an error, it makes sense for the server to return an appropriate 4xx or 5xx response, and not a 301 Redirection to a 200 OK response. I don't know why Microsoft added this option to ASP.NET's Custom Error Page feature, but fortunately you don't need to use it.

I recommend using IIS Manager to generate your web.config file for you. As for handling the error, open your Global.asax.cs file and add a method for Application_Error, then call Server.GetLastError() from within.

1 Comment

Dai i have to write details and stack trace about error on ErrorPage.aspx
1

In the Global.asax

void Application_Error(object sender, EventArgs e) 
{    
    Session["error"] = Server.GetLastError().InnerException; 
}
void Session_Start(object sender, EventArgs e) 
{      
    Session["error"] = null;
}

In the Error_Page Page_Load event

if (Session["error"] != null)
{
    // You have the error, do what you want
}

1 Comment

This won't work as the Session object is not available when the Application_Error event is triggered.
1

Use Elmah dll for displaying your error with nice UI. You can maintain log using this DLL.

Comments

0

You can access the error using Server.GetLastError;

var exception = Server.GetLastError();
  if (exception != null)
    {
       //Display Information here
    }

For more information : HttpServerUtility.GetLastError Method

1 Comment

is this efficient way..means i have to place above on every page..should i handle any errors on master page only..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.