9

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.

Iñaki Elcoro
  • 2,153
  • 1
  • 18
  • 33

6 Answers6

18

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"; }

Paul Thomas
  • 181
  • 1
  • 2
  • This indeed solved my problem as well. Empty internet application -> No layout defined. Good spot! – Rebecca Feb 17 '12 at 16:13
  • I've been wrestling with this for ages and setting the Layout got this all working perfectly for me despite the fact I have a _ViewStart.cshtml defining the default. Thanks a bunch! – ProNotion Dec 05 '13 at 21:36
  • Yep, this resolved my issue straight away. Awesome job @Paul Thomas! – jason Oct 29 '15 at 15:03
8

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;
    }
}
Community
  • 1
  • 1
Kenon
  • 96
  • 1
  • 2
6

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?

Iñaki Elcoro
  • 2,153
  • 1
  • 18
  • 33
  • Wow. Nice find, I don't know how long I would have been searching if I wouldn't have found your self-answer :D – Shtong Feb 08 '11 at 23:30
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>
ram
  • 41
  • 1
  • @Bavarious 6 Years later, I am experiencing this problem and the contents of my Error.cshtml is already at 1KB but I still can't solve this very annoying problem. – Harold_Finch Jun 05 '17 at 15:25
1

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.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • I will try again when I get home, however those are the steps I followed – Iñaki Elcoro Jan 30 '11 at 17:00
  • I have reproduced exactly those steps, and I only get the http status 500 page – Iñaki Elcoro Jan 30 '11 at 20:24
  • @iCe, are you running this on Cassini? Also check in the EventLog of your machine. Maybe there's another problem you are having. – Darin Dimitrov Jan 30 '11 at 20:25
  • As said is not working inside "Cassini" the builtin asp.net development server and not inside IIS too. Also, there are'nt any strange events logged in the eventlog – Iñaki Elcoro Jan 30 '11 at 20:34
  • @iCE, there must be something else which is wrong with your setup and unfortunately I am unable to say what because I am unable to reproduce it. For me it works as expected in Cassini and IIS, using ASP.NET MVC 3 RTM. – Darin Dimitrov Jan 30 '11 at 20:47
0

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.

Frank Hoffman
  • 887
  • 1
  • 11
  • 16