我有AdminError.aspx和Error.aspx页面,我想在出现异常时显示AdminError.aspx。我需要这两个文件。
为什么这不起作用?
<customErrors mode="On" redirectMode="ResponseRedirect" defaultRedirect="AdminError.aspx" />
而是始终显示Error.aspx。
我该怎么办?
答案 0 :(得分:1)
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();
}
}
查看this similar question以了解更多信息。
答案 1 :(得分:1)
谢谢,
我想我找到了解决方案。
我正在使用HandleErrorWithELMAHAttribute(How to get ELMAH to work with ASP.NET MVC [HandleError] attribute?) 在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);
}
我注意到它可以使用和不使用customErrors标记中的redirectMode和defaultRedirect属性。