0

I want to pass an identifier to a custom error page in ASP.NET MVC.

I have the web.config set up to redirect to an error page when there is an status 500 error. I don't know how to pass an error id from the application code that throws the exception (also sets HTTP Status of 500) back to the error page that is configured.

It's easy if I just used Status 200 and returned a view from the Error method of the controller and did all the logic in there, however the Status code must be 500, so that's not an acceptable solution.

I also do not and will not use session for this. I'd like to use query string or view data.

Not a duplicate. This is about error handling (status code 500) and exceptions. 404 is about Page Not Found

user1060500
  • 1,505
  • 1
  • 21
  • 40
  • 1
    possible duplicate of [ASP.NET MVC 404 Error Handling](http://stackoverflow.com/questions/717628/asp-net-mvc-404-error-handling) – Erik Philips Mar 12 '15 at 01:15
  • Not a duplicate. This is about error handling (status code 500) and exceptions. 404 is about Page Not Found – user1060500 Mar 12 '15 at 01:17
  • @user1060500 That post Erik references is still about exception handling. Seems like [one of the answers](http://stackoverflow.com/a/10106444/1810243) could be adapted or perhaps [this other question/answer](http://stackoverflow.com/a/22400027/1810243). – MikeSmithDev Mar 12 '15 at 02:23

1 Answers1

2

I don't know a better way to customized information to error page configured in web.config. This is what I normally do when i need an error redirection:

I'll create an error handler action filter:

public class MyErrorHandler: HandleErrorAttribute
{
    public override void OnException(ExceptionContext filterContext)
    {
        if (filterContext.Exception != null)
        {
            filterContext.HttpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
            filterContext.ExceptionHandled = true;
            filterContext.Result = new RedirectResult("/error?error_id=123456", true);
            return;
        }
        base.OnException(filterContext);
    }
}

Within the Exception filter, you may try to handle differnet error with different error id, or whatever you prefer to alter the exception.

Then apply it onto controller or controler class:

[MyErrorHandler]
public class HomeController : Controller
{
    ....
}

And you will get a Http status 500 and will be redirected to:

http://<host>/error?error_id=123456
Alex
  • 161
  • 1
  • 4