未显示 MVC 自定义错误

本文关键字:错误 自定义 MVC 显示 | 更新日期: 2023-09-27 18:25:13

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

我的网络配置文件有:

<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".`

我错过了什么?

未显示 MVC 自定义错误

通过在 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="仅远程"仅激活远程客户端的自定义错误,

您应该有一个错误控制器,其中包含显示错误视图的操作。并且该操作应具有以下注释:

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

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