2012-04-23 4 views
1

MVC3とElmahをリンクしようとしています。すべてがうまく動作し、すべてのエラーが処理され、記録されますが、私はカスタムエラーを持つユーザーのフロントエンドに問題があります。私は、グローバルフィルタMVC3 + Elmah = web.configのdefaultredirectが機能しない

public class ElmahHandleErrorAttribute : System.Web.Mvc.HandleErrorAttribute 
{ 
    public override void OnException(ExceptionContext context) 
    { 
     base.OnException(context); 

     var e = context.Exception; 
     if (!context.ExceptionHandled // if unhandled, will be logged anyhow 
      || RaiseErrorSignal(e)  // prefer signaling, if possible 
      || IsFiltered(context))  // filtered? 
      return; 

     LogException(e); 
    } 

    private static bool RaiseErrorSignal(Exception e) 
    { 
     var context = HttpContext.Current; 
     if (context == null) 
      return false; 
     var signal = ErrorSignal.FromContext(context); 
     if (signal == null) 
      return false; 
     signal.Raise(e, context); 
     return true; 
    } 

    private static bool IsFiltered(ExceptionContext context) 
    { 
     var config = context.HttpContext.GetSection("elmah/errorFilter") 
        as ErrorFilterConfiguration; 

     if (config == null) 
      return false; 

     var testContext = new ErrorFilterModule.AssertionHelperContext(
            context.Exception, HttpContext.Current); 

     return config.Assertion.Test(testContext); 
    } 

    private static void LogException(Exception e) 
    { 
     var context = HttpContext.Current; 
     ErrorLog.GetDefault(context).Log(new Error(e, context)); 
    } 
} 

を書いて、私は

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

Global.asaxの

にフィルタを登録しますが、例外が発生したとき、ハンドラは、それが、web.configファイルからのdoesnt使用defaultRedirectでパスを処理していました。これは、WebConfigの

<customErrors mode="On" defaultRedirect="~/Home/NeutralError"> 
    <error statusCode="404" redirect="~/Home/NeutralErrorNotFound" /> 
</customErrors> 

任意のアイデアをして〜/共有/ Error.cshtml

私のビューを見ていますか? :|デフォルトとして

答えて

1

HandleError属性がcustomErrorsセクションに設定defaultRedirect特性に基づいて、共有フォルダではなく作品にErrorビューを探します。 HandleErrorに別のビュー名を検索するように指示できますが、あなたのケースでは他のアクションにリダイレクトしたいと思います。私はあなたのonExceptionを方法であなたのベースクラスのビューを設定する

public override void OnException(ExceptionContext context) 
{ 
    base.OnException(context); 

    var e = context.Exception; 
    if (!context.ExceptionHandled // if unhandled, will be logged anyhow 
     || RaiseErrorSignal(e)  // prefer signaling, if possible 
     || IsFiltered(context))  // filtered? 
     return; 

    LogException(e); 

    // newly added 
    if (context.Exception is HttpException) 
    { 
    if(((HttpException)context.Exception).GetHttpCode() == 404) 
     context.Result = new RedirectResult("~/Home/NeutralErrorNotFound"); 
    } 
    context.Result = new RedirectResult("~/Home/NeutralError"); 
} 
+0

残念ながら、条件が満たされ、OnExceptionメソッドから返されるため、残念ながら正常ではありません – MichalGrunwald

0

が私のためにこれを修正するように見えた、(テストしていない)、これは動作します願っています。

public override void OnException(ExceptionContext context) 
    { 
     base.View = "~/views/error/errorindex.cshtml"; // New code here... 
     base.OnException(context); 

     var e = context.Exception; 
     if (!context.ExceptionHandled // if unhandled, will be logged anyhow 
      || RaiseErrorSignal(e)  // prefer signaling, if possible 
      || IsFiltered(context))  // filtered? 
      return; 

     LogException(e); 
    } 
関連する問題