我有AdminError.aspx和Error.aspx页面,我想在出现异常时显示AdminError.aspx。两份文件我都需要。
为什么这个不起作用?
<customErrors mode="On" redirectMode="ResponseRedirect" defaultRedirect="AdminError.aspx" />相反,始终显示Error.aspx。
我能做什么?
发布于 2009-11-24 06:10:05
谢谢,
我想我找到了一个解决方案。
我使用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属性。
发布于 2009-11-24 05:52:08
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以了解更多信息。
https://stackoverflow.com/questions/1786239
复制相似问题