Question

Coming from a desktop background I'm not sure exactly how to pass the exceptions I have caught to an Error page in order to avoid the standard exception screen being seen by my users.

My general question is how do I pass the exception from page X to my Error page in ASP.net?

Was it helpful?

Solution

I suggest using the customErrors section in the web.config:

   <customErrors mode="RemoteOnly" defaultRedirect="/error.html">
      <error statusCode="403" redirect="/accessdenied.html" />
      <error statusCode="404" redirect="/pagenotfound.html" />
   </customErrors>

And then using ELMAH to email and/or log the error.

OTHER TIPS

The pattern I use is to log the error in a try/catch block (using log4net), then do a response.redirect to a simple error page. This assumes you don't need to show any error details.

If you need the exception details on a separate page, you might want to look at Server.GetLastError. I use that in global.asax (in the Application_Error event) to log unhandled exceptions and redirect to an error page.

We've had good luck capturing exceptions in the Global.asax Application_Error event, storing them in session, and redirecting to our error page. Alternately you could encode the error message and pass it to the error page in the querystring.

You can also get the exception from

Server.GetLastError();

Use the custom error pages in asp.net, you can find it in the customError section in the web.config

We capture the exception in the Global.asax file, store it in Session, the user is then redirected to the Error Page where we grab the exception for our Session variable and display the Message information to the user.

    protected void Application_Error(object sender, EventArgs e)
    {
        Exception ex = Server.GetLastError();
        this.Session[CacheProvider.ToCacheKey(CacheKeys.LastError)] = ex;
    }

We do log the error message prior to displaying it the user.

I think you can use the global.asax -- Application_Exception handler to catch the exception and then store it for displaying in an error page.

But actually, your error page shouldn't contains code that might cause just another error. It should be simple "Oops! something went wrong" page.

If you want details on the error, use Windows' events viewer or ELMAH or employ some logging mechanism.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top