Home > redirect to > mvc onexception redirect to error page

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: Custom 404 error pages When a resource does redirect to not exist (either static or dynamic) we should return a 404 HTTP status code. Ideally we should return something a little friendlier to our site visitors than the error pages built in to ASP.NET/IIS, perhaps offering some advice on why the resource may not exist or providing an option to search the site. For the purposes of this blog post, onexception redirect to my custom 404 page is very simple, but you can see some really nice examples here. 404 Page Not Found

404 Page Not Found

I created a new ASP.NET MVC 5 application using the standard template in Visual Studio. If I run the site and try to navigate to a resource that does not exist e.g. /foo/bar, I'll get the standard ASP.NET 404 page with the following information: Server Error in '/' Application. The resource cannot be found. Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly. Requested URL: /foo/bar Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.33440 Not exactly friendly, is it? In this case the error was raised by ASP.NET MVC because it could not find a matching controller and/or action that matched the specified URL. In order to set up a custom

 

Related content

application error redirect to action

Application Error Redirect To Action table id toc tbody tr td div id toctitle Contents div ul li a href Application Error Global Asax Redirect a li li a href Redirect To Action With Query String a li li a href Redirect To Action Not Working a li li a href Redirect To Action From Javascript a li ul td tr tbody table p here for a quick overview of the site Help Center Detailed answers relatedl to any questions you might have Meta Discuss p h id Application Error Global Asax Redirect p the workings and policies of this

asp.net mvc redirect to error page

Asp net Mvc Redirect To Error Page table id toc tbody tr td div id toctitle Contents div ul li a href Mvc Redirect To Error Page On Exception a li li a href Redirect To Error Page From Global asax Mvc a li li a href Aspnet Mvc Source a li li a href Mvc Redirect To Error View a li ul td tr tbody table p 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 others relatedl by IIS Ideally and I expect

asp.net redirect to an error page

Asp net Redirect To An Error Page table id toc tbody tr td div id toctitle Contents div ul li a href Redirect To Error Page Mvc a li li a href Application error Redirect To Error Page a li li a href Exception Handling In Asp Net C With Example a li ul td tr tbody table p p p One relatedl games Xbox games PC p h id Exception Handling In Asp Net C With Example p games Windows games Windows phone games Entertainment All asp net error handling Entertainment Movies TV Music Business Education Business Students asp

onexception redirect to error view

Onexception Redirect To Error View table id toc tbody tr td div id toctitle Contents div ul li a href Onexception Mvc a li li a href Handleerrorattribute Redirect To Action a li li a href Exception Handling In Mvc Example a li li a href Handleerrorattribute Example a li ul td tr tbody table p here for a quick overview of the site Help Center Detailed answers to relatedl any questions you might have Meta Discuss the workings p h id Onexception Mvc p and policies of this site About Us Learn more about Stack Overflow onexception c the