2012-08-07 26 views
62

私はStackOverflowで十数個の同様の質問を読んだが、私はこれを把握していないようだ。 web.configのカスタムエラーノードとHandleErrorAttributeに関して、Error.cshtmlはどのように呼び出されますか?最終的には、この質問に対する答えは、ASP.NET MVCのエラー処理に関するいくつかの質問のうちの1つに対する回答かもしれません。しかし、事実は、私はどちらがわからないのですか。ASP.NET MVCでError.cshtmlがどのように呼び出されますか?

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

これはHandleErrorAttributeグローバルアクションフィルタとして登録します。あなたのGlobal.asaxのインサイド

答えて

82

次の方法があります。つまり、このハンドラはすべてのコントローラアクションに自動的に適用されます。それでは、この属性は、ソースコードを見ることで実装されているかを見てみましょう:

[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。カスタムエラーを有効にしている場合は、エラービューをレンダリングして、例外スタックトレースとその他の有用な情報を含むモデルを渡します。

+0

私はまだASP.NET MVCの新機能ですが、各ビューはコントローラの動作に対応している必要があります。ここでコントローラとアクションは何ですか?また、共有のビューにはどうすればよいでしょうか?このメカニズムは、エラービューで割り込みとスワップを行うためのものです。 – LJM

+2

はい、各ビューはコントローラアクションに対応している必要があります。この場合のコントローラーアクションは実行されており、例外をスローします。コントローラーアクションでもかまいません。この例外は、グローバルアクションフィルタ(この場合は例外フィルタ)によってインターセプトされ、エラービューがレンダリングされます。このアクションはコントローラのアクション内に例外がスローされるので、このアクションは決して戻りません。このステージでは実行を停止し、エラーハンドラへの実行を処理し、ビューをレンダリングします。 –

+0

ありがとうございます。そうです。 – LJM

関連する問題