I am trying to get the view back without redirecting the user based on certain errors that may occur from my application, I want to handle errors + register them inside my base controller, I do not want the error to propagate to my Global.asax method - Application_Error () since I want this method to handle any other errors inside my application, for example a user enters a dummy URL, has anyone found a way around this?
NOTE. I left my code with comments, since I had a workaround for some problems, it also shows that I have a few exceptions for a possible descriptor ...
EDIT: if I throw a RedirectToAction in this OnException override, everything works as expected, but I only want to return the view and not redirect ...
My base controller method:
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; //log 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 filterContext.Result = View(new RouteValueDictionary (new { area = "", controller = "Error", action = "NoAccess" })); //} //advise subsequent exception filters not to interfere and stop // asp.net from showing yellow screen of death filterContext.ExceptionHandled = true; //erase any output already generated filterContext.HttpContext.Response.Clear(); //base.OnException(filterContext); }
This method should handle any other errors that may appear in my application, I do not want the errors described above to be processed inside my application Application_Error ()
protected void Application_Error() { Exception exception = Server.GetLastError(); // Log the exception. var logger = Container.Get<ILoggingService>(); logger.Error(User.Identity.Name, ExceptionHelper.BuildWebExceptionMessage(exception)); Response.Clear(); HttpException httpException = exception as HttpException; RouteData routeData = new RouteData(); routeData.Values.Add("controller", "Error"); //if (httpException == null) //{ routeData.Values.Add("action", "PublicError"); //} //else //It an Http Exception, Let handle it. //{ // switch (httpException.GetHttpCode()) // { // case 404: // // Page not found. // routeData.Values.Add("action", "HttpError404"); // break; // case 500: // // Server error. // routeData.Values.Add("action", "HttpError500"); // break; // // Here you can handle Views to other error codes. // // I choose a General error template // default: // routeData.Values.Add("action", "General"); // break; // } //} // Pass exception details to the target error View. routeData.Values.Add("error", exception); // Clear the error on server. Server.ClearError(); // Avoid IIS7 getting in the middle Response.TrySkipIisCustomErrors = true; // Call target Controller and pass the routeData. IController errorController = new ErrorController(); errorController.Execute(new RequestContext( new HttpContextWrapper(Context), routeData)); }
c # exception-handling error-handling asp.net-mvc-2
Haroon Jun 12 '11 at 20:11 2011-06-12 20:11
source share