自定义错误重定向页面与ELMAH不兼容。

4
我已经在我的应用程序中实现了ELMAH日志记录,一切正常,只是自定义页面重定向不起作用。 我已经在web.config中添加了以下行:
<customErrors mode="On" defaultRedirect="/Bug/ViewBug">
    <error statusCode="401" redirect="/Bug/AccessDenied"/>
    <error statusCode="403" redirect="/Bug/AccessDenied"/>
     <error statusCode="404" redirect="/Bug/PageNotFound" />
    <error statusCode="500" redirect="/Bug/InternalServerError" />
</customErrors>

Controllers\BugController.cs文件中,以下是需要注意的代码行。
public class BugController : Controller
    {
        public ActionResult ViewBug()
        {
            return View();
        }
        public ActionResult AccessDenied()
        {
            return View();
        }
        public ActionResult PageNotFound()
        {
            return View();
        }
        public ActionResult InternalServerError()
        {
            return View();
        }
    }

根据控制器,我也创建了相应的视图。

我使用这个教程实现了ELMAH日志记录。

HandleErrorActionInvoker.cs

 public class HandleErrorActionInvoker : ControllerActionInvoker
    {
        private readonly IExceptionFilter filter;
        public HandleErrorActionInvoker(IExceptionFilter filter)
        {
            if (filter == null)
            {
                throw new ArgumentNullException("filter");
            }
            this.filter = filter;
        }

        protected override FilterInfo GetFilters(
        ControllerContext controllerContext,
        ActionDescriptor actionDescriptor)
        {
            var filterInfo =
            base.GetFilters(controllerContext,
            actionDescriptor);
            filterInfo.ExceptionFilters.Add(this.filter);
            return filterInfo;
        }
    }

HandleErrorAttribute.cs

 public class HandleErrorAttribute : System.Web.Mvc.HandleErrorAttribute
    {
        public override void OnException(ExceptionContext context)
        {
            base.OnException(context);
            var e = context.Exception;
            // if unhandled, will be logged anyhow | // prefer signaling, if possible | // filtered?
            if (!context.ExceptionHandled || RaiseErrorSignal(e) || IsFiltered(context)) 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));
        }
    }

HandleErrorControllerFactory.cs

public class HandleErrorControllerFactory : DefaultControllerFactory
    {
        public override IController CreateController(RequestContext requestContext, string controllerName)
        {
            var controller = base.CreateController(requestContext, controllerName);
            var c = controller as Controller;
            if (c != null)
            {
                c.ActionInvoker = new HandleErrorActionInvoker(new HandleErrorAttribute());
            }
            return controller;
        }
    }

生成测试错误。
public ActionResult Index()
{
    // Throw a test error so that we can see that it is handled by Elmah
    // To test go to the ~/elmah.axd page to see if the error is being logged correctly
    throw new Exception("A test exception for ELMAH");
    return View();
}

问题:

这会显示错误页面Shared\Error.cshtml,而不是Bug\ViewBug

我需要在web.config中更改什么才能使用customErrors?


请参见:https://dev59.com/RWQn5IYBdhLWcg3wn4NS - solidau
1个回答

2

请检查您的global.asax文件。您应该在RegisterGlobalFilters中注册自己的HandleError过滤器,而不是默认的HandleError过滤器。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接