asp.net mvc 3 handleerror global filter always shows IIS status 500 page

I have tried everything, even uninstalling asp.net mvc3, and I can’t get HandleError global filter working.

I have set up the HandleError filter in the Global.asax:

 public static void RegisterGlobalFilters(GlobalFilterCollection filters)
 {
     filters.Add(new HandleErrorAttribute());
 }

Also I have CustomErrors enabled (it does not matter if i set defaultRedirect=”Error” or not, I think that is in the docs because is needed for older versions of mvc):
<customErrors mode="On" />

Trying to navigate through the page until the error gets raised, wether you do from localhost or using the hostname, inside the development server or IIS 7.5, it always redirects to a standard status 500 page, instead of my custom Error.cshtml view that I have created in Shared. Here is Error view code:
@model System.Web.Mvc.HandleErrorInfo

@{
    ViewBag.Title = "Oooops";
}

<h2>Ooops Something really bad happened!</h2>

Also I have noted that if I create a new ASP.NET MVC3 project and then select “Internet Application” template, and just enabling customErrors in that project, then the HandleError filter starts working just fine, however using the empty MVC3 template does not.

I want to clarify, that indeed I can see the error view being processing when debugging, however the browser always display Error 500 page.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

To fix this all you need to do is edit your Error.cshtml page and make sure that you have set the Layout property correctly. In an Empty MVC 3 Application this is set to NULL, which causes the HTTP 500 internal server error.

I fixed this issue by simply adding:

@{
Layout = "~/Views/Shared/_Layout.cshtml";
}

Method 2

I ran into this same issue. I found this post: 500 Internal Server Error in ASP.NET MVC and tried unchecking “Friendly Http Errors” and my HandleErrors attribute started working as expected in IE 8 with IE 7 compatibility mode checked (and started working like it does in Chrome by default). I don’t think this is a valid solution for a deployed app, but perhaps there’s a way to extend the HandleError attribute so that it redirects to an “Error” controller instead of just the default Error view.

UPDATE: This post shows that when an HTTP Status 500 is returned, IE handles it awkwardly. A way to get around it, is to set the Status to 200, then everything seems to work OK (even in IE).

public class CustomHandleErrorAttribute : HandleErrorAttribute
{
    public override void OnException(ExceptionContext filterContext)
    {    
        base.OnException(filterContext);

        //https://stackoverflow.com/questions/5137627/mvc-handleerror-returns-error-500-and-view-error-page
        //https://stackoverflow.com/questions/183316/asp-net-mvc-handleerror
        filterContext.HttpContext.Response.StatusCode = 200;
    }
}

Method 3

Well this is hilarous, finally found the answer, this machine has Internet Explorer 9 beta installed and is a difference in behavior when handling the status 500 that the HandleError attribute sets before showing the Error view.

I have tried with other browsers and is working fine:
Chrome vs IE 9 Beta

What I cannot undestarnd now is why the “Internet Appication” template was working though.

Anyone knows where I can post the bug? Should it go to the asp.net mvc team or IE 9 team? How do I contact them?

Method 4

Found the issue – the error page – make sure you put some dummy content – to make the content size atleast 1KB.

Here are the contents of Error.cshtml

<!DOCTYPE html>
<html>
<head>
    <title>Error</title>
</head>
<body>
    <h2>
        Sorry, an error occurred while processing your request. @DateTime.Now
    </h2>
    <div>
    ..........................................................................
    ..........................................................................
    ..........................................................................
    ..........................................................................
    ..........................................................................
    ..........................................................................
    ..........................................................................
    ..........................................................................
    ..........................................................................
    ..........................................................................
    ..........................................................................
    </div>
</body>
</html>

Method 5

Here are the steps I’ve used and which worked for me:

  1. Create a new ASP.NET MVC 3 project using the Empty template
  2. Add a new controller called HomeController with the following contents (you don’t need any view => we are throwing an exception anyway):
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            throw new Exception("error");
        }
    }
  3. Turn on custom errors on web.config by adding the following section:
    <customErrors mode="On">
    </customErrors>
  4. Run the site and the ~/Views/Shared/Error.cshtml view which was generated by the template will be shown.

Method 6

I think I found a way around this. Replace your Error.cshtml and _Layout.cshtml with the ones generated from the Internet or Intranet templates.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x