ASP.NET MVC에서 Error.cshtml은 어떻게 호출됩니까?
StackOverflow에서 비슷한 질문을 열두 개 읽었지만 이해하지 못하는 것 같습니다. web.config 및 HandleErrorAttribute의 사용자 지정 오류 노드와 관련하여 Error.cshtml이 어떻게 호출됩니까? 궁극적으로이 질문에 대한 대답은 ASP.NET MVC 오류 처리와 관련된 여러 질문 중 하나에 대한 대답 일 수 있습니다. 그러나 문제의 사실은 어느 것이인지 모르겠다는 것입니다.
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;
}
}
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.
참고URL : https://stackoverflow.com/questions/11851328/how-is-error-cshtml-called-in-asp-net-mvc
'IT TIP' 카테고리의 다른 글
변수가 반복 가능하지만 문자열이 아니라는 것을 알려주는 방법 (0) | 2020.10.24 |
---|---|
1000 개의 Entity Framework 개체를 만들 때 SaveChanges ()를 언제 호출해야합니까? (0) | 2020.10.24 |
어레이가 동일하게 유지 될 확률은 얼마입니까? (0) | 2020.10.24 |
-D 매개 변수 또는 환경 변수를 Spark 작업에 전달하는 방법은 무엇입니까? (0) | 2020.10.24 |
왜 instanceof가 babel-node 아래에있는 Error 서브 클래스의 인스턴스에서 작동하지 않습니까? (0) | 2020.10.24 |