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

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

我做了如下设置:
(1)web.config:
<customErrors mode="On" redirectMode="ResponseRewrite" defaultRedirect="Error/defaulterror">
<error statusCode="404" redirect="Error/404"/>
</customErrors>

(2)ErrorController.cs:
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与asp.net mv 路由是不兼容的。所以如果使用“ResponseRewrite”的时候defaultRedirect或者 Error tag里的redirect 必须是实际存在的页面才可以。如下面的配置:

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


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!

转载于:https://www.cnblogs.com/adandelion/archive/2011/12/28/2304671.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值