ASP.NET MVC 5无法将500错误重定向至自定义错误页面
ASP.NET MVC自定义错误页面500错误循环问题
我正在为ASP.NET MVC应用创建自定义错误页面,已完成以下配置:
1. 错误控制器Action
public ActionResult Error(int id, string message, string stackTrace) { Response.StatusCode = id; ViewBag.Message = message; ViewBag.StackTrace = stackTrace; return View("~/Views/PaginaErro/PaginaErro.cshtml"); }
2. Global.asax.cs中的Application_Error方法
protected void Application_Error() { Exception exception = Server.GetLastError(); var httpException = exception as HttpException; int httpCode = httpException?.GetHttpCode() ?? (int)HttpStatusCode.InternalServerError; Server.ClearError(); Response.Redirect("~/ErrorPage/Error?id=" + httpCode + "&message=" + HttpUtility.UrlEncode(exception.Message) + "&stackTrace=" + HttpUtility.UrlEncode(exception.StackTrace)); }
3. Web.config配置
<customErrors mode="On" defaultRedirect="~/ErrorPage/Error"> <error redirect="~/ErrorPage/Error/404" statusCode="404"/> <error redirect="~/ErrorPage/Error/500" statusCode="500"/> </customErrors>
4. 路由配置(RouteConfig)
public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapMvcAttributeRoutes(); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); } }
当前问题
- 404错误可正常跳转:触发Application_Error后进入ErrorPageController的Error方法。
- 500错误陷入循环:执行Response.Redirect后无法进入ErrorPageController,反复触发Application_Error。
- 500错误的异常信息:
The view 'Error' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Areas/Erp/Views/AjusteDeEstoque/Error.aspx
~/Areas/Erp/Views/AjusteDeEstoque/Error.ascx
~/Areas/Erp/Views/Shar...
未创建名为Error的视图,不清楚系统为何要查找该视图。
问题原因分析
- 自定义错误页配置冲突:Web.config中
<customErrors>设置mode="On",同时在Application_Error里使用Response.Redirect,500错误发生时,ASP.NET会先尝试按customErrors配置跳转,跳转过程中可能因路由或视图问题再次触发错误,导致循环。 - MVC默认视图查找逻辑:当某个Action抛出未处理异常时,MVC会默认尝试查找对应控制器下的
Error视图,而非直接跳转到自定义错误控制器,这就是系统查找~/Areas/Erp/Views/AjusteDeEstoque/Error.aspx的原因。
解决方案
方案1:替换Response.Redirect为服务器内部跳转
Response.Redirect会发送302重定向,引发新请求可能再次触发错误;改用Server.TransferRequest实现服务器内部跳转,避免循环。修改Application_Error方法:
protected void Application_Error() { Exception exception = Server.GetLastError(); var httpException = exception as HttpException; int httpCode = httpException?.GetHttpCode() ?? (int)HttpStatusCode.InternalServerError; Server.ClearError(); // 构建路由数据直接调用错误控制器 var routeData = new RouteData(); routeData.Values["controller"] = "ErrorPage"; routeData.Values["action"] = "Error"; routeData.Values["id"] = httpCode; routeData.Values["message"] = HttpUtility.UrlEncode(exception.Message); routeData.Values["stackTrace"] = HttpUtility.UrlEncode(exception.StackTrace); IController controller = new ErrorPageController(); controller.Execute(new RequestContext(new HttpContextWrapper(Context), routeData)); }
方案2:调整Web.config的customErrors配置
移除具体错误节点,统一由Application_Error处理,避免重复跳转:
<customErrors mode="RemoteOnly" defaultRedirect="~/ErrorPage/Error"> </customErrors>
方案3:确认错误视图路径正确性
检查~/Views/PaginaErro/PaginaErro.cshtml是否存在,若错误控制器属于某个区域,需确保视图路径包含区域名称(如~/Areas/[区域名]/Views/PaginaErro/PaginaErro.cshtml)。
内容的提问来源于stack exchange,提问作者Thiago Barbieri Catarina
相关产品推荐
相关产品推荐

