0

I want to be able to control a redirect to a different page or return a different view on certain handled errors. I can create custom pages with status 404, 403, 418, and it displays the page fine, but when I try to return 500 it overrides it with a default page. I've tried the normal solutions. Using TrySkipIisCustomErrors to get by IIS intercepting the 500 error. I have no filter.cs and register no filters in application_Start(). I've tried adding and removing [HandleError] from the method and that doesn't make a difference.

[NonAction]
    [HandleError]
    private ActionResult error(Exception e)
    {
        Response.StatusCode = 500;
        Response.StatusDescription = "Internal server error: " + e.Message;
        Response.TrySkipIisCustomErrors = true;

        ErrorModel errorModel = new ErrorModel();
        errorModel.error = e.Message;
        return View(viewName: "error", model: errorModel);
    }

That's the method that the error is redirected to and returns the view to be displayed. I can change the first line to other StatusCodes and it works fine, but 500 doesn't.

ccwscott
  • 51
  • 1
  • 8

1 Answers1

0

Turns out it was a weird fluke with Razor. The RenderBody() adds <body> tags if none are in the document, apparently unless you return 500 in your status header. Then it doesn't add the <body> tags, and the HTML returns malformed which was causing my problem.

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
ccwscott
  • 51
  • 1
  • 8