public static ActionResult HandleException(HttpApplication context, Exception exception) { if (exception != null) { var httpException = exception as HttpException; int statusCode = httpException != null ? httpException.GetHttpCode() : 500; //check the statusCode against the config if (!Routing.StatusCodesToIgnoreFromWebConfig.Contains(statusCode)) { context.Response.Clear(); context.Server.ClearError(); context.Response.StatusCode = statusCode; var routeData = CreateRoute(context, exception); context.Response.TrySkipIisCustomErrors = true; IController errorHandlerController = new ErrorHandlerController(); var rc = new RequestContext(new HttpContextWrapper(context.Context), routeData); errorHandlerController.Execute(rc); } } return null; }
// Handle exceptions that occur outside the MVC layer protected void Application_Error(object sender, EventArgs args) { var ex = Server.GetLastError(); if (ex is HttpException) { // We're already handling these with handlers // in httpErrors return; } var httpContext = ((MvcApplication)sender).Context; var currentController = " "; var currentAction = " "; var currentRouteData = RouteTable.Routes.GetRouteData(new HttpContextWrapper(httpContext)); if (currentRouteData != null) { if (currentRouteData.Values["controller"] != null && !string.IsNullOrEmpty(currentRouteData.Values["controller"].ToString())) { currentController = currentRouteData.Values["controller"].ToString(); } if (currentRouteData.Values["action"] != null && !string.IsNullOrEmpty(currentRouteData.Values["action"].ToString())) { currentAction = currentRouteData.Values["action"].ToString(); } } var controller = new ErrorHandlerController(); var routeData = new RouteData(); httpContext.ClearError(); httpContext.Response.Clear(); httpContext.Response.Write("This exception hit the global exception handler in MvcApplication.Application_Error."); // Don't do this in production, it's like this for // demonstration purposes. Uncomment this to see how it would // actually behave in production. //httpContext.Response.StatusCode = ex is HttpException // ? ((HttpException)ex).GetHttpCode() // : 500; httpContext.Response.TrySkipIisCustomErrors = true; routeData.Values["controller"] = "ErrorHandler"; var action = "ApplicationError"; routeData.Values["action"] = action; // Set the Controller's model controller.ViewData.Model = new HandleErrorInfo(ex, currentController, currentAction); ((IController)controller).Execute( new RequestContext(new HttpContextWrapper(httpContext), routeData)); }
private void ExecuteErrorHandlerController(string action, HttpContext httpContext, Exception exception) { var routeData = new RouteData(); routeData.Values["controller"] = "ErrorHandler"; routeData.Values["action"] = action; routeData.Values["exception"] = exception; using (Controller controller = new ErrorHandlerController()) { ((IController)controller).Execute(new RequestContext(new HttpContextWrapper(httpContext), routeData)); } }
protected void Application_Error() { Exception ex = Server.GetLastError(); Context.Response.Clear(); Context.ClearError(); RequestContext requestContext = ((MvcHandler)Context.CurrentHandler).RequestContext; if (requestContext.HttpContext.Request.IsAjaxRequest()) { Context.Response.Write("Ajax request failed due to server error!"); Context.Response.End(); } else { var routeData = new RouteData(); routeData.Values["controller"] = "ErrorHandler"; routeData.Values["action"] = "InternalException"; routeData.Values.Add("url", Context.Request.Url.OriginalString); routeData.Values.Add("exception", ex); //var httpException = ex as HttpException; //if (httpException != null) //{ // switch (httpException.GetHttpCode()) // { // case 404: // routeData.Values["action"] = "NotFound"; // break; // case 403: // routeData.Values["action"] = "GenericError"; // break; // case 500: // routeData.Values["action"] = "InternalException"; // break; // } //} IController controller = new ErrorHandlerController(); controller.Execute(new RequestContext(new HttpContextWrapper(Context), routeData)); } }
protected void Application_Error(object sender, EventArgs e) { Exception exception = Server.GetLastError(); RouteData routeData = new RouteData(); routeData.Values.Add("ErrorMsg", exception.Message); routeData.Values.Add("StackTrace", exception.StackTrace); routeData.Values.Add("Controller", "ErrorHandler"); if (exception is DbException) { routeData.Values.Add("Action", "DatabaseError"); } else if (exception is HttpException) { routeData.Values.Add("ExUrl", HttpContext.Current.Request.Url); if ((exception as HttpException).GetHttpCode() == 404) { routeData.Values.Add("Action", "PageError"); } else { routeData.Values.Add("Action", "CommonError"); } } else { routeData.Values.Add("Action", "CommonError"); } Response.Clear(); Server.ClearError(); try { IController exceptionControl = new ErrorHandlerController(); exceptionControl.Execute(new RequestContext(new HttpContextWrapper(Context), routeData)); } catch (Exception) { } }