1

MVC3 と Elmah をリンクしようとしています。すべてがうまく機能し、すべてのエラーが処理されてログに記録されますが、カスタム エラーを持つユーザーのフロントエンドに問題があります。グローバルフィルターを書きました

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

Global.asaxにフィルターを登録しました

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

ただし、例外が発生すると、ハンドラーはそれを処理しましたが、web.config からの defaultredirect パスを使用しません。~/Shared/Error.cshtml のビュー

私のwebconfig

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

何か案は?: |

4

2 に答える 2

1

デフォルトのHandleError属性は、共有フォルダー内のビューを検索し、セクションで設定されErrorたプロパティに基づいて機能しません。別のビュー名を探すように指示できますが、あなたの場合、他のアクションにリダイレクトしたいと思います。これがうまくいくことを願っています(テストされていません)、defaultRedirectcustomErrorsHandleError

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");
}
于 2012-04-23T18:22:51.863 に答える
0

OnException メソッドで基本クラスのビューを設定すると、これが修正されたようです。

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);
    }
于 2012-05-29T07:24:14.787 に答える