如何在Error.cshtml ASP.NET MVC叫什么名字? [英] How is Error.cshtml called in ASP.NET MVC?

查看:117
本文介绍了如何在Error.cshtml ASP.NET MVC叫什么名字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读过关于计算器十几类似的问题,但我似乎无法理解这一点。至于在web.config和HandleErrorAttribute的自定义错误节点,请问Error.cshtml曾经被调用?最终,这个问题的答案可能是答案的那些几个问题已经在那里对ASP.NET MVC的错误处理之一。但是,问题其实是,我不知道是哪一个。

I've read a dozen similar questions on StackOverflow, but I can't seem to grasp this. With regards to the custom errors node in the web.config and the HandleErrorAttribute, how does the Error.cshtml ever get called? Ultimately the answer to this question may be the answer to one of those several questions already out there regarding ASP.NET MVC error handling. But, fact of the matter is, I don't know which one.

推荐答案

您的Global.asax里面你有以下方式:

Inside your Global.asax you have the following method:

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

该注册HandleErrorAttribute作为全球行动的过滤器。这意味着,这个处理程序将自动适用于所有控制器动作。现在,让我们来看看如何属性通过查看源$ C ​​$ C实现的:

This registers the HandleErrorAttribute as global action filter. This means that this handler is automatically applied to all controller actions. Now let's take a look at how this attribute is implemented by looking at the source code:

[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;
    }
}

源$ C ​​$ C包含注释,比言自明了。它检查的第一件事情是你是否已经在你的web.config启用自定义错误(即&LT;的customErrors模式=ON&GT; )。如果你还没有它什么也不做=> YSOD。如果您已启用自定义错误,那么它呈现它传递包含异常堆栈跟踪和其他有用信息模型中的错误观点。

The source code contains comments and is more than self explanatory. The first thing it checks is whether you have enabled custom errors in your web.config (i.e. <customErrors mode="On">). If you haven't it does nothing => YSOD. If you have enabled custom errors then it renders the Error view passing it a model containing the exception stacktrace and other useful information.

这篇关于如何在Error.cshtml ASP.NET MVC叫什么名字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆