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 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;
}
}
Here are the steps I've used and which worked for me:
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");
}
}
Turn on custom errors on web.config by adding the following section:
<customErrors mode="On">
</customErrors>
Run the site and the ~/Views/Shared/Error.cshtml
view which was generated by the template will be shown.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With