如何在ASP.NET MVC中调用Error.cshtml?


76

我已经在StackOverflow上阅读了十二个类似的问题,但是我似乎无法理解这一点。关于web.config和HandleErrorAttribute中的自定义错误节点,如何调用Error.cshtml?最终,这个问题的答案可能是已经存在的有关ASP.NET MVC错误处理的几个问题之一。但是,事实是,我不知道是哪一个。

Answers:


90

在您的Global.asax内部,您可以使用以下方法:

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

这会将HandleErrorAttribute注册为全局操作过滤器。这意味着此处理程序将自动应用于所有控制器操作。现在,通过查看源代码来看看如何实现此属性:

[SuppressMessage("Microsoft.Performance", "CA1813:AvoidUnsealedAttributes", Justification = "This attribute is AllowMultiple = true and users might want to override behavior.")]
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
public class HandleErrorAttribute : FilterAttribute, IExceptionFilter {

    private const string _defaultView = "Error";

    private readonly object _typeId = new object();

    private Type _exceptionType = typeof(Exception);
    private string _master;
    private string _view;

    public Type ExceptionType {
        get {
            return _exceptionType;
        }
        set {
            if (value == null) {
                throw new ArgumentNullException("value");
            }
            if (!typeof(Exception).IsAssignableFrom(value)) {
                throw new ArgumentException(String.Format(CultureInfo.CurrentCulture,
                    MvcResources.ExceptionViewAttribute_NonExceptionType, value.FullName));
            }

            _exceptionType = value;
        }
    }

    public string Master {
        get {
            return _master ?? String.Empty;
        }
        set {
            _master = value;
        }
    }

    public override object TypeId {
        get {
            return _typeId;
        }
    }

    public string View {
        get {
            return (!String.IsNullOrEmpty(_view)) ? _view : _defaultView;
        }
        set {
            _view = value;
        }
    }

    public virtual void OnException(ExceptionContext filterContext) {
        if (filterContext == null) {
            throw new ArgumentNullException("filterContext");
        }
        if (filterContext.IsChildAction) {
            return;
        }

        // If custom errors are disabled, we need to let the normal ASP.NET exception handler
        // execute so that the user can see useful debugging information.
        if (filterContext.ExceptionHandled || !filterContext.HttpContext.IsCustomErrorEnabled) {
            return;
        }

        Exception exception = filterContext.Exception;

        // If this is not an HTTP 500 (for example, if somebody throws an HTTP 404 from an action method),
        // ignore it.
        if (new HttpException(null, exception).GetHttpCode() != 500) {
            return;
        }

        if (!ExceptionType.IsInstanceOfType(exception)) {
            return;
        }

        string controllerName = (string)filterContext.RouteData.Values["controller"];
        string actionName = (string)filterContext.RouteData.Values["action"];
        HandleErrorInfo model = new HandleErrorInfo(filterContext.Exception, controllerName, actionName);
        filterContext.Result = new ViewResult {
            ViewName = View,
            MasterName = Master,
            ViewData = new ViewDataDictionary<HandleErrorInfo>(model),
            TempData = filterContext.Controller.TempData
        };
        filterContext.ExceptionHandled = true;
        filterContext.HttpContext.Response.Clear();
        filterContext.HttpContext.Response.StatusCode = 500;

        // Certain versions of IIS will sometimes use their own error page when
        // they detect a server error. Setting this property indicates that we
        // want it to try to render ASP.NET MVC's error page instead.
        filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
    }
}

源代码包含注释,并且不仅仅是自我解释。它检查的第一件事是您是否在web.config中启用了自定义错误(即<customErrors mode="On">)。如果没有,它什么也不做=> YSOD。如果启用了自定义错误,则它将呈现“错误”视图,并向其传递一个包含异常堆栈跟踪和其他有用信息的模型。


我对ASP.NET MVC还是很陌生,但是我认为每个View必须与某个控制器的动作相对应。这里的控制器和动作是什么,如何知道要转到“共享”视图?看起来这种机制在“错误”视图中可以中断和交换。
LJM

3
是的,每个视图必须对应于一个控制器动作。在这种情况下,控制器动作是正在执行的动作,它会引发异常。可以是任何控制器动作。全局操作过滤器(在本例中为异常过滤器)会拦截此异常,并呈现“错误”视图。由于异常是在控制器动作中引发的,因此该动作永不返回,因此它仅在此阶段停止执行并将处理交给错误处理程序处理,该错误处理程序进而呈现视图。
达林·迪米特洛夫

12
这在MVC 5.1中有所改变吗?在我看来,HandleErrorAttribute默认情况下已注册(无需将其添加到RegisterGlobalFilters中的过滤器列表中),并且我们不再需要启用自定义错误。
约翰尼·奥什卡

1
在MVC 5中答案是否已更改?我没有得到错误页面。我只看到YSOD。我有<customError mode =“ On”>。还缺少什么?
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.