【问题标题】:MVC custom errors not showingMVC 自定义错误未显示
【发布时间】:2014-09-30 19:37:34
【问题描述】:

我在视图中设置了 /shared/error.cshtml 页面以显示更友好的错误消息。但是当用户遇到错误时它不会显示。

我的 web.config 文件有:

<system.web>
<customErrors mode="On" />

我的 global.asax.cs 有:

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new HandleErrorAttribute ());
    }

但我得到的错误信息仍然是基本的

`Server Error in '/' Application.

Runtime Error

Description: An application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being viewed. 

Details: To enable the details of this specific error message to be viewable on the local server machine, please create a <customErrors> tag within a "web.config" configuration file located in the root directory of the current web application. This <customErrors> tag should then have its "mode" attribute set to "RemoteOnly". To enable the details to be viewable on remote machines, please set "mode" to "Off".`

我错过了什么?

【问题讨论】:

  • 您到底想达到什么目的?如果您不指定自定义错误页面,那么将 CustomErrors 设置为 On 是没有意义的,对吧?基本上它说发生了错误并且显示自定义错误页面时出现错误...
  • 我希望它转到共享布局中指定的error.cshtml。当您创建 MVC 应用程序时,它默认会执行此操作,但我们是从头开始构建的,所以我不知道如何
  • 您应该有一个带有显示此视图的操作的 ErrorController。并且动作应该有以下注释:[HandleError(View = "Error", ExceptionType = typeof(Exception))]
  • 您应该有一个带有显示此视图的操作的 ErrorController。并且动作应该有以下注释:

标签: c# asp.net-mvc asp.net-mvc-4


【解决方案1】:

通过在下添加以下行来激活 Web.config 中的自定义错误

<system.web>:

<customErrors mode="On" defaultRedirect="/Error">
  <error redirect="/Error/NotFound" statusCode="404"/>
  <error redirect="/Error/InternalServerError" statusCode="500"/>
</customErrors>

注意:您可以将 mode=”Off” 设置为禁用自定义错误,这在开发或调试时可能会有所帮助。设置 mode=”RemoteOnly” 只为远程客户端激活自定义错误,

【讨论】:

  • 如果我将错误指向控制器或任何自定义视图,我不是。我正在尝试使用默认功能。另外,即使我确实添加了它。它不起作用。恕我直言,关于 SA 的另一个答案的措辞对我没有帮助,尤其是当他们正在做一些更定制的事情时
【解决方案2】:

您应该有一个带有显示错误视图的操作的 ErrorController。并且动作应该有以下注释:

[HandleError(View = "Error", ExceptionType = typeof(Exception))]

【讨论】:

    【解决方案3】:

    想通了,一些动态内容在出现错误时没有被加载,因为它没有使用基本控制器,因此错误处理抛出了一个错误

    【讨论】:

      猜你喜欢
      • 2011-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多