Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 786
Iñaki Elcoro Avatar asked Jan 30 '11 11:01

Iñaki Elcoro


2 Answers

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;
    }
}
like image 111
Kenon Avatar answered Oct 28 '22 09:10

Kenon


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.

like image 25
Darin Dimitrov Avatar answered Oct 28 '22 08:10

Darin Dimitrov