1

I'm working on making my ASP.NET MVC apps handle HTTP status codes correctly and when needed display a view for them. I'm avoiding all of the out of the box error page handling since it isn't semantic (e.g. the framework always performs a 302 redirect with customErrors).

I have an AuthorizeAttribute and ActionFilterAttribute which are both working correctly and my custom view is displayed. However, my HandleErrorAttribute isn't working quite right.

public override void OnException(ExceptionContext context) {
    HttpStatusCode httpStatusCode = HttpStatusCode.InternalServerError;

    var e = context.Exception;

    if (e is HttpException) {
        var httpException = (HttpException)e;
        httpStatusCode = (HttpStatusCode)httpException.GetHttpCode();
    }

    context.Result = MyResultBuilder.SetViewByHttpCode(context.HttpContext.Request, httpStatusCode, context.Controller.ViewData.ModelState);
}

I am setting the Result property, but at some point the pipeline is overriding my result as I end up with a Yellow Screen of Death (YSOD) instead of the view result that I defined.

How do I set a custom view result in the HandleErrorAttribute?

Justin Helgerson
  • 24,900
  • 17
  • 97
  • 124

1 Answers1

1

Try setting the ExceptionHandled property to true;

Ventsyslav Raikov
  • 6,882
  • 1
  • 25
  • 28
  • Thanks for the reply Bond. The one issue with that approach is something that I failed to point out in my original post: Elmah. I'm using Elmah for my error logging and according to http://stackoverflow.com/questions/766610/how-to-get-elmah-to-work-with-asp-net-mvc-handleerror-attribute the suggested approach is to signal errors so they can be handled later in the pipeline. Any suggestions for working around that or am I out of luck? – Justin Helgerson May 12 '14 at 14:52
  • Why can't you use the answer to the question you linked? I.e directly logging the error to ELMAH through the API? – Ventsyslav Raikov May 12 '14 at 15:00