gpt4 book ai didi

asp.net-mvc-3 - ASP.NET MVC 中如何调用 Error.cshtml?

转载 作者:行者123 更新时间:2023-12-03 05:20:49 25 4
gpt4 key购买 nike

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

最佳答案

在 Global.asax 中,您有以下方法:

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

这会将 HandleErrorAttribute 注册为全局操作过滤器。这意味着该处理程序会自动应用于所有 Controller 操作。现在我们通过源码看一下这个属性是如何实现的:

[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。如果您启用了自定义错误,那么它会呈现错误 View ,并向其传递包含异常堆栈跟踪和其他有用信息的模型。

关于asp.net-mvc-3 - ASP.NET MVC 中如何调用 Error.cshtml?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11851328/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com