Global error handling (outside of controller) in ASP.NET MVC?

As MVC is built on top of asp. Net you should be able to define a global error page in web. Config just like you could in web forms eg customErrors mode="On" defaultRedirect="~/ErrorHandler.

As MVC is built on top of asp. Net you should be able to define a global error page in web. Config, just like you could in web forms eg.

Enable customErrors: and redirect to a custom error controller: HandleError public class ErrorController : BaseController { public ErrorController () { } public ActionResult Index () { Response. StatusCode = (int)HttpStatusCode. InternalServerError; return View ("Error"); } public ActionResult Unauthorized () { Response.

StatusCode = (int)HttpStatusCode. Unauthorized; return View ("Error401"); } public ActionResult NotFound () { string url = GetStaticRoute (Request. QueryString"aspxerrorpath"?Request.

Path); if (!string. IsNullOrEmpty (url)) { Notify ("Due to a new web site design the page you were looking for no longer exists. ", false); return new MovedPermanentlyResult (url); } Response.

StatusCode = (int)HttpStatusCode. NotFound; return View ("Error404"); } }.

As far as what page to display, you'll need to create a customErrors section in your web. Config and set it up for whatever status codes you want to handle. Example: As far as logging exceptions, I would recommend using ELMAH.It integrates nicely with ASP.

NET MVC sites.

Yes, HandleError won't actually do anything unless you have customErrors on in the config. – swilliams Aug 31 '09 at 17:32 I still need to be able to retrieve and log the exception details. Preferrably, I would be able to do this in the controller action that the defaultRedirect points to.

– iguanaNet Aug 31 '09 at 17:35 @iguananet That is what ELMAH does. If you want to do it separately then that's a whole different matter, but it works very well. I would highly recommend giving it a try.

– Joseph Aug 31 '09 at 18:04 @Joseph-- Thanks, but I'd like to find the simplest way to do it without any dependencies or new things to introduce into my project. – iguanaNet Aug 31 '09 at 18:08.

You can create a Filter that looks for an Exception in the OnActionExecuted method: AttributeUsage(AttributeTargets. Class | AttributeTargets. Method) public class WatchExceptionAttribute : ActionFilterAttribute { public override void OnActionExecuted(ActionExecutedContext filterContext) { if (filterContext.

Exception! = null) { // do your thing here. } } } Then you can put WatchException on a Controller or Action Method, and it will let log exceptions.

If you have a lot of Controllers, that might be tedious, so if you have a common Base Controller you can override OnActionExecuted there and do the same thing. I prefer the filter method.

You may find this article useful: blog.ploeh.dk/2009/12/01/GlobalErrorHand....

1 This was an excellent answer! – Peter Morris Aug 10 at 11:18.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions