gpt4 book ai didi

asp.net-mvc-3 - 自定义错误重定向页面不适用于 ELMAH

转载 作者:行者123 更新时间:2023-12-01 04:09:44 24 4
gpt4 key购买 nike

我在我的应用程序中实现了 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();
}
}

并且根据 Controller ,我也为相同的 View 创建了 View 。

我有 使用 this tutorial 实现了 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,我应该改变什么?

最佳答案

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

关于asp.net-mvc-3 - 自定义错误重定向页面不适用于 ELMAH,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6991690/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com