c# - MVC3 HandleError 不显示错误页面

标签 c# asp.net asp.net-mvc-3

我一直在遵循 HandleError 属性的指南: blogs.msdn.com 我这样使用(AccountController):

[HandleError(View="ErasErrorPage")]
        public ActionResult Index()
        {
            ViewBag.admins = _accountMapper.GetAdmins(false);
            ViewBag.members = _accountMapper.GetMembers(false);
            ViewBag.Countries = _countryMapper.GetCountries();

            return View();
        }

此代码抛出异常,因为 _accountMapper.GetAdmins(false)由于 System.Data.EntityException 而失败。

我已将 ErasErrorPage View 放在我的共享文件夹中并添加了 <customErrors mode="On"/>但 ErasErrorPage 没有出现。发生错误时我得到的只是一个黄色的死亡屏幕:

Error 显然,将模式设置为“Off”或“RemoteOnly”并不能解决问题。

有人知道为什么这行不通吗?


编辑

如果我直接冲浪到http://localhost:3527/Account/Index ,我确实得到了正确的 ErasErrorPage,但我不想要那个。我希望网站在某处抛出异常时自动重定向到该页面。这可能吗?


EDIT2

我已经把 [HandleError(View="ErasErrorPage")]每一个之前的属性 Public ActionResult methodName() { ... }方法,我仍然收到蓝屏死机提示我需要将模式更改为“关闭”或“仅远程”...

最佳答案

确保 ErasErrorPage View 本身不会抛出异常。以下是我执行的步骤,对我来说效果很好:

  1. 使用默认的 Visual Studio 向导创建一个新的 ASP.NET MVC 3 项目
  2. 添加具有以下内容的 ~/Views/Shared/ErasErrorPage.cshtml:

    @{
        Layout = null;
    }
    <!DOCTYPE html>
    <html>
    <head>
        <title>ErasErrorPage</title>
    </head>
    <body>
        <div>
            OOPS
        </div>
    </body>
    </html>
    
  3. 将 HomeContoller 修改为如下所示:

    public class HomeController : Controller
    {
        [HandleError(View = "ErasErrorPage")]
        public ActionResult Index()
        {
            throw new Exception("oops");
        }
    }
    
  4. 在 web.config 中放置:

    <customErrors mode="On" />
    
  5. 导航到 /Home/Index => 自定义错误页面按预期显示

关于c# - MVC3 HandleError 不显示错误页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5947388/

相关文章:

c# - *.csproj 中的 usevshostingprocess 是什么?

jQuery datetimepicker 太多递归错误

asp.net-mvc-3 - 根据asp.net mvc3中的下拉列表数据获取列表

c# - ASP.NET MVC - 将查询字符串值添加到 Controller 中的 RedirectToAction

c# - 在 Visual Studio 2015 RC 中使用 intelliTest 记录每个运行的测试场景

javascript - 从 Javascript 和 Mithril.JS 使用 asp.net Web 服务 - post/get 请求的最佳方法/库是什么?

asp.net - Global.asax Application_Error 是异步的吗?

sql - 无法删除数据库,因为它正在使用 + EF 代码优先

c# - .net XmlSerializer,忽略基类属性

c# - 如何在 .NET 6/C# 10 中将 List<String?> 转换为 List<String>?