我有 AdminError.aspx 和 Error.aspx 页面,我想在出现异常时显示 AdminError.aspx。我需要这两个文件。
为什么这不起作用?
<customErrors mode="On" redirectMode="ResponseRedirect" defaultRedirect="AdminError.aspx" />
而是始终显示 Error.aspx。
我能做些什么?
我有 AdminError.aspx 和 Error.aspx 页面,我想在出现异常时显示 AdminError.aspx。我需要这两个文件。
为什么这不起作用?
<customErrors mode="On" redirectMode="ResponseRedirect" defaultRedirect="AdminError.aspx" />
而是始终显示 Error.aspx。
我能做些什么?
Asp.net mvc 提供 [HandleError] 属性来处理此类需求,您可以根据特定的错误类型指定要重定向到的不同错误页面(视图)。它非常灵活,建议这样做。
例如
[HandleError(ExceptionType = typeof(NullReferenceException),
View = "NullError")]
[HandleError(ExceptionType = typeof(SecurityException),
View = "SecurityError")]
public class HomeController : Controller
{
public ActionResult Index()
{
throw new NullReferenceException();
}
public ActionResult About()
{
return View();
}
}
查看这个类似的问题以了解更多信息。
谢谢,
我想我找到了解决办法。
我正在使用 HandleErrorWithELMAHAttribute (如何让 ELMAH 与 ASP.NET MVC [HandleError] 属性一起使用?)并在 OnException 方法中设置了我的视图:
public override void OnException(ExceptionContext context)
{
View = "AdminError"; // this is my view
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);
}
I've noticed that it works with and without redirectMode and defaultRedirect attributes from customErrors tag.