void Application_Error(object sender, EventArgs e) { HttpContext httpContext = ((MvcApplication)sender).Context; Exception exception = Server.GetLastError() ?? new ApplicationException("Unknown error"); //write log entry Logger.AddEntry(exception); //clear error state httpContext.ClearError(); httpContext.Response.Clear(); //prepare and execute error controller var errorController = new Controllers.ErrorController(); var routeData = new RouteData(); routeData.Values["action"] = "Index"; routeData.Values["controller"] = "Error"; errorController.ViewData.Model = new HandleErrorInfo(exception, "ErrorController", "Index"); // this row with error - Fixed ((IController)errorController).Execute (new RequestContext (new HttpContextWrapper (httpContext), routeData)); }
/// <summary> /// Handles error on Application. /// </summary> /// <param name="sender">Contains sender object.</param> /// <param name="e">Contains EventArgs object.</param> protected void Application_Error(object sender, EventArgs e) { HttpContext httpContext = ((MvcApplication)sender).Context; //LogException(httpContext, Server.GetLastError()); httpContext.ClearError(); httpContext.Response.Clear(); if (httpContext.Request.Headers["X-Requested-With"] != null && !string.IsNullOrEmpty(Convert.ToString(httpContext.Request.Headers["X-Requested-With"])) && Convert.ToString(httpContext.Request.Headers["X-Requested-With"]) == "XMLHttpRequest") { // For Ajax call exceptions UrlHelper url = new UrlHelper(httpContext.Request.RequestContext); var error = new { error = "AjaxError", errorurl = url.Action("Error", "Error", string.Empty, url.RequestContext.HttpContext.Request.Url.Scheme) }; Response.ContentType = "application/json; charset=utf-8"; Response.Write(new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(error)); Response.End(); } else { var controller = new Controllers.ErrorController(); var routeData = new RouteData(); routeData.Values["controller"] = "Error"; routeData.Values["action"] = "Error"; ((IController)controller).Execute(new RequestContext(new HttpContextWrapper(httpContext), routeData)); } }
/// <summary> /// Called when a request matches this controller, but no method with the specified action name is found in the controller. /// </summary> /// <param name="actionName">The name of the attempted action.</param> protected override void HandleUnknownAction(string actionName) { // Avoid IIS7 getting in the middle Response.TrySkipIisCustomErrors = true; IController errorsController = new Controllers.ErrorController(); var errorRoute = new RouteData(); errorRoute.Values.Add("controller", "error"); errorRoute.Values.Add("action", "notfound"); errorRoute.Values.Add("url", HttpContext.Request.Url.OriginalString); errorsController.Execute(new RequestContext(HttpContext, errorRoute)); }
private void RedirectToErrorAction(HttpContext context, string actionName) { context.Server.ClearError(); context.Response.TrySkipIisCustomErrors = true; context.Response.Clear(); RouteData routeData = new RouteData(); routeData.Values.Add("controller", "error"); routeData.Values.Add("action", actionName); IController errorController = new Controllers.ErrorController(); var httpContextWrapper = new HttpContextWrapper(context); var requestContext = new RequestContext(httpContextWrapper, routeData); errorController.Execute(requestContext); }
public override void OnException(System.Web.Mvc.ExceptionContext filterContext) { filterContext.ExceptionHandled = true; var routeData = new RouteData(); routeData.Values["controller"] = "Error"; routeData.Values["action"] = "Error"; routeData.DataTokens["area"] = "app"; IController errorsController = new Controllers.ErrorController(); var rc = new RequestContext(new HttpContextWrapper(HttpContext.Current), routeData); errorsController.Execute(rc); base.OnException(filterContext); }
void Application_Error(object sender, EventArgs e) { var exception = context.Server.GetLastError(); TryLogException(exception); var httpException = exception as HttpException; context.Response.Clear(); context.Server.ClearError(); var routeData = new RouteData(); routeData.Values["controller"] = "Error"; routeData.Values["action"] = "General"; routeData.Values["exception"] = exception; context.Response.StatusCode = 500; if (httpException != null) { context.Response.StatusCode = httpException.GetHttpCode(); switch (context.Response.StatusCode) { case 401: routeData.Values["action"] = "Http401"; break; case 403: routeData.Values["action"] = "Http403"; break; case 404: routeData.Values["action"] = "Http404"; break; } } IController errorsController = new Controllers.ErrorController(); var requestedContext = new RequestContext(new HttpContextWrapper(context.Context), routeData); errorsController.Execute(requestedContext); }