Mvc Onexception Redirect To Error Page
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss onexception redirect to error view the workings and policies of this site About Us Learn more about onexception c# Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions handleerrorattribute redirect to action Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other.
Apicontroller Onexception
Join them; it only takes a minute: Sign up Asp.net mvc override OnException in base controller keeps propogating to Application_Error up vote 35 down vote favorite 14 I am trying to return a view not issue a redirect to the user based on certain errors that could occur from my application, I want to handle the errors + mvc error handling best practice log them inside my base controller, I do not want the error to propagate up to my Global.asax - Appliacation_Error() method as I want this method to handle any other errors inside my app e.g. user enters a bogus URL, has anyone found a way around this? NOTE: I have left my commented code as I had a workaround for some issues, this also shows I have multiple exceptions to possible handle... EDIT: If I issue a RedirectToAction within this OnException override everything works as expected, but I only want to return the view and no redirection... My base controller method is: protected override void OnException(ExceptionContext filterContext) { //dont interfere if the exception is already handled if (filterContext.ExceptionHandled) return; //let the next request know what went wrong filterContext.Controller.TempData["exception"] = filterContext.Exception; //logg exception _logging.Error(User.Identity.Name, ExceptionHelper.BuildWebExceptionMessage(filterContext.Exception)); //set up redirect to my global error handler //if (filterContext.Exception.GetType() == typeof(NoAccessException)) // filterContext.Result = View(new RouteValueDictionary // (new { area = "", controller = "Error", action = "PublicError" })); //else { //Only return view, no need for redirection filt
for a quick overview of the site Help Center Detailed answers exceptioncontext result to any questions you might have Meta Discuss the workings
Handleerror Attribute Mvc
and policies of this site About Us Learn more about Stack Overflow the company Business Learn
Mvc Application_error
more about hiring developers or posting ads with us Code Review Questions Tags Users Badges Unanswered Ask Question _ Code Review Stack Exchange is a http://stackoverflow.com/questions/6324368/asp-net-mvc-override-onexception-in-base-controller-keeps-propogating-to-applica question and answer site for peer programmer code reviews. Join them; it only takes a minute: Sign up Here's how it works: Anybody can ask a question Anybody can answer The best answers are voted up and rise to the top Error Handling - Controller's OnException and Application_Error up vote http://codereview.stackexchange.com/questions/60759/error-handling-controllers-onexception-and-application-error 11 down vote favorite 7 I'm pretty new to MVC. Below is the code that I have added to an existing MVC 5 application. The code is using log4net to log any run-time error/exception. NOTE: I could override OnException() in MVC filter "HandleErrorAttribute" but I preferred to have this functionality in my custom Controller base class. Can anyone please review and provide feedback? BaseController.cs public class BaseController: Controller { private log4net.ILog logger; protected override void OnException(ExceptionContext filterContext) { //Log error logger = log4net.LogManager.GetLogger(filterContext.Controller.ToString()); logger.Error(filterContext.Exception.Message, filterContext.Exception); //If the request is AJAX return JSON else redirect user to Error view. if (filterContext.HttpContext.Request.Headers["X-Requested-With"] == "XMLHttpRequest") { //Return JSON filterContext.Result = new JsonResult { JsonRequestBehavior = JsonRequestBehavior.AllowGet, Data = new { error = true, message = "Sorry, an error occurred while processing your request." } }; } else { //Redirect user to error page filterContext.ExceptionHandled = true; filterContext.Result = this.RedirectToAction("Index", "Error"); } base.OnException(filterContext); }
you're not alone. It's surprisingly difficult to do this correctly, not helped by the fact that some errors are handled by ASP.NET and http://benfoster.io/blog/aspnet-mvc-custom-error-pages others by IIS. Ideally (and I expect such is the case with some other frameworks/servers) we would just configure our custom error pages in one place and it would just work, no matter how/where the error was raised. Something like: