ASP.NET MVC 里redirectMode="ResponseRewrite" 时候无法使用 Controller 来设置特定的错误页面。

ASP.NET MVC 里redirectMode="ResponseRewrite" 时候无法使用 Controller 来设置特定的错误页面。



<customErrors mode="On" redirectMode="ResponseRewrite" defaultRedirect="Error/defaulterror">

<error statusCode="404" redirect="Error/404"/>



public ActionResult Error(string ErrorCode)


ViewData["ErrorCOde"] = ErrorCode;

return View();



Server Error in '/' Application.


The resource cannot be found.

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

Requested URL: /

查询了资料发现ResponseRewrite的时候系统使用的是Server.Transfer的方式来显示错误信息的。而Server.Transfer与 mv 路由是不兼容的。所以如果使用“ResponseRewrite”的时候defaultRedirect或者 Error tag里的redirect 必须是实际存在的页面才可以。如下面的配置:

<customErrors mode="On" redirectMode="ResponseRewrite" defaultRedirect="~/Common/Error/DefaultError.aspx">

<error statusCode="404" redirect="~/Common/Error/404.aspx"/>


It is important to note for anyone trying to do this in an MVC application that ResponseRewrite uses Server.Transfer behind the scenes. Therefore, the defaultRedirect must correspond to a legitimate file on the file system. Apparently, Server.Transfer is not compatible with MVC routes, therefore, if your error page is served by a controller action, Server.Transfer is going to look for /Error/Whatever, not find it on the file system, and return a generic 404 error page!