Global.asax 中的应用程序错误重定向到错误页面失败 (MVC)
Redirect to Error Page Fail on Application Error in Global.asax (MVC)
当在 Global.asax 中遇到应用程序错误时,我很难尝试重定向到我的错误处理控制器。
这是我的 RouteCollection
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"DefaultError",
"{controller}/{action}/{id}",
new { controller = "HndlError", action = "AllErrors", id = UrlParameter.Optional }
);
我的 Global.asax
Application_Error
有这个
this.Context.Response.Clear();
this.Context.ClearError();
//this.Response.Redirect("~/HndlError/AllErrors") // This one Works !!!
this.Response.RedirectToRoute( new { controller = "HndlError",
action = "AllErrors", id = ErrorMessage }); // This does not Work
this.Response.End();
我的控制器动作在使用 Response.Redirect
时确实被击中,但是 returns 一个空白页面 RedirectToRoute
。进行了更多搜索,然后发现了这个 GEM !
Beware of ResponseRedirectToRoute in MVC 3
这是否意味着它不能在 MVC3 中工作或者我错过了什么?请帮助。
谢谢
尝试以下操作:
protected void Application_Error(object sender, EventArgs e)
{
HttpContext ctx = HttpContext.Current;
ctx.Response.Clear();
RequestContext rc =((MvcHandler)ctx.CurrentHandler).RequestContext;
rc.RouteData.Values["action"] = "AllErrors";
rc.RouteData.Values["controller"] = "HndlError";
rc.RouteData.Values["id"] = ErrorMessage ;
IControllerFactory factory = ControllerBuilder.Current.GetControllerFactory();
IController controller = factory.CreateController(rc, "HndlError");
controller.Execute(rc);
ctx.Server.ClearError();
}
当在 Global.asax 中遇到应用程序错误时,我很难尝试重定向到我的错误处理控制器。
这是我的 RouteCollection
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"DefaultError",
"{controller}/{action}/{id}",
new { controller = "HndlError", action = "AllErrors", id = UrlParameter.Optional }
);
我的 Global.asax
Application_Error
有这个
this.Context.Response.Clear();
this.Context.ClearError();
//this.Response.Redirect("~/HndlError/AllErrors") // This one Works !!!
this.Response.RedirectToRoute( new { controller = "HndlError",
action = "AllErrors", id = ErrorMessage }); // This does not Work
this.Response.End();
我的控制器动作在使用 Response.Redirect
时确实被击中,但是 returns 一个空白页面 RedirectToRoute
。进行了更多搜索,然后发现了这个 GEM !
Beware of ResponseRedirectToRoute in MVC 3
这是否意味着它不能在 MVC3 中工作或者我错过了什么?请帮助。
谢谢
尝试以下操作:
protected void Application_Error(object sender, EventArgs e)
{
HttpContext ctx = HttpContext.Current;
ctx.Response.Clear();
RequestContext rc =((MvcHandler)ctx.CurrentHandler).RequestContext;
rc.RouteData.Values["action"] = "AllErrors";
rc.RouteData.Values["controller"] = "HndlError";
rc.RouteData.Values["id"] = ErrorMessage ;
IControllerFactory factory = ControllerBuilder.Current.GetControllerFactory();
IController controller = factory.CreateController(rc, "HndlError");
controller.Execute(rc);
ctx.Server.ClearError();
}