• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ASP.NETMVC里redirectMode="ResponseRewrite"时候无法使用Controller来设置 ...

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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!


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap