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

3
  • 1
    possible duplicate of ASP.NET MVC 404 Error Handling Commented Mar 12, 2015 at 1:15
  • Not a duplicate. This is about error handling (status code 500) and exceptions. 404 is about Page Not Found Commented Mar 12, 2015 at 1:17
  • @user1060500 That post Erik references is still about exception handling. Seems like one of the answers could be adapted or perhaps this other question/answer. Commented Mar 12, 2015 at 2:23

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

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.