Asp.net Mvc Global Error Handling
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About exception handling in asp.net mvc 5 Us Learn more about Stack Overflow the company Business Learn more about hiring exception handling in mvc 5 developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join
Exception Handling In Mvc 4
the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up ASP.NET MVC 5 error handling https://dusted.codes/demystifying-aspnet-mvc-5-error-pages-and-error-logging up vote 12 down vote favorite 4 We want to handle 403 errors, 404 errors, all errors due to a MySpecialDomainException and provide a default error page for all other errors (including errors in the IIS configuration!). All errors should return proper Razor views, it would be very nice to have an ErrorController in front of the views. E.g. something like this: public class ErrorController : http://stackoverflow.com/questions/21993758/asp-net-mvc-5-error-handling Controller { public ViewResult NotFound () { return View(); } public ViewResult Forbidden () { return View(); } public ViewResult Default () { var ex = ObtainExceptionFromSomewhere(); if(ex is MySpecialDomainException) return View("MySpecialDomainException", new ErrorModel { Exception = ex }); return View("GeneralError", new ErrorModel { Exception = ex }); } } Currently you find many different ways to do that on the www, some most probably outdated. Among those: Controller.OnException() Error filter customErrors element in web.config Handling in Global.asax's Application_Error Q1: What is the recommended way to fulfill our requirements with ASP.NET MVC 5? Also we want to catch errors occurring in the IIS host. Q2: To prevent that IIS has to handle any 404s we thought about adding a default route matching all possible URLs - is this recommendable? Better to register instead for IIS' 404s as well? Q3: Is it even possible to register an IIS error page which goes back to a controller, or is IIS capable of ASPX / static HTML only? c# asp.net-mvc error-handling asp.net-mvc-5 asp.net-mvc-5.1 share|improve this question edited Jul 26 at 6:09 Mukesh Ram 2,7373625 asked Feb 24 '14 at 16:46 D.R. 6,14672271 I'm curious.. How ex
our site. It's not fair to return HTTP status code 200 on error, even if at the http://www.milevis.com/Tips/Details/45 same time we return a view, explaining that an error occurred. if http://blog.ploeh.dk/2009/12/01/GlobalErrorHandlinginASP.NETMVC/ the user types in an incorrect address (the most frequent user fault), we should return HTTP status code 404 and not return or redirect to a View, where status code 200 will be returned. Here we come to the MVC global error handling basic rules considering custom errors asp.net mvc settings in Web.config and global error filters in Global.asax.cs: 1. Have HandleErrorAttribute registered in your Global.asax.cs. public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new HandleErrorAttribute()); } In fact, it is there by default, called in Application_Start(). Don't remove it. Otherwise the framework will display it's fallback message "Server Error in '/' Application...". By default above filter captures HTTP errors 500 and displays global error handling the /Views/Shared/Error.cshtml view, but you can customize it setting the View property like filters.Add(new HandleErrorAttribute()){View="AnotherView"}. 2. Have the customErrors settings in Web.config.
dreaded Yellow Screen of Death. The most prominently described technique involves attributing either you Controller or a single Controller action, like we see in the AccountController created by the Visual Studio project template.[HandleError] public class AccountController : Controller { } That sure is easy, but I don't like it for a number of reasons: Even though you can derive from HandleErrorAttribute, it's impossible to inject any dependencies into Attributes because they must be fully constructed at compile-time. That makes it really difficult to log errors to an interface. It violates the DRY principle. Although it can be applied at the Controller level, I still must remember to attribute all of my Controllers with the HandleErrorAttribute. Another approach is to override Controller.OnException. That solves the DI problem, but not the DRY problem. Attentive readers may now point out that I can define a base Controller that implements the proper error handling, and require that all my Controllers derive from this base Controller, but that doesn't satisfy me: First of all, it still violates the DRY principle. Whether I have to remember to apply a [HandleError] attribute or derive from a : MyBaseController amounts to the same thing. I (and all my team members) have to remember this always. It's unnecessary project friction. The second thing that bugs me about this approach is that I really do favor composition over inheritance. Error handling is a cross-cutting concern, so why should all my Controllers have to derive from a base controller to enable this? That is absurd. Fortunately, ASP.NET MVC offers a third way. The key lies in the Controller base class and how it deals with IExceptionFilters (which HandleErrorAttribute implements). When a Controller action is invoked, this actually happens through an IActionInvoker. The default ControllerActionInvoker is responsible for gathering the list of IExceptionFilters, so the first thing we can do is to derive from that and override its GetFilters method:public class ErrorHandlingActionInvoker : ControllerActionInvoker { private readonly IExcep