zoukankan      html  css  js  c++  java
  • 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!

    诗词在线
    http://www.chinapoesy.com
    诗词在线 |唐诗|宋词|元曲|现代诗歌|外国诗歌
    126在线阅读网
    http://www.Read126.cn
    126在线阅读网 人物传记、古典名著、历史书籍。。。
  • 相关阅读:
    基于 HTML5 WebGL 构建智能数字化城市 3D 全景
    基于 H5 + WebGL 实现 3D 可视化地铁系统
    基于 HTML5 WebGL 的 3D 科幻风机
    基于 HTML5 + WebGL 的太阳系 3D 展示系统
    HT Vue 集成
    基于 HTML5 + WebGL 的地铁 3D 可视化系统
    基于 HTML5 WebGL 和 VR 技术的 3D 机房数据中心可视化
    String、StringBuffer和StringBuilder的区别
    Python--Numpy基础
    python中的next()以及iter()函数
  • 原文地址:https://www.cnblogs.com/adandelion/p/2304671.html
Copyright © 2011-2022 走看看