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 的默认重定向路径。它在 ~/Shared/Error.cshtml 中查看视图

我在 webconfig 中

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

有任何想法吗?: |

4

2 回答 2

1

HandleError默认属性Error在共享文件夹中查找视图,而不是根据部分defaultRedirect设置的属性工作customErrors。您可以告诉HandleError寻找不同的视图名称,但我认为在您的情况下您想重定向到其他操作。我希望这会起作用(未经测试),

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 回答