您可以使用上述HandleError属性标记的解决方案。
捕捉错误的另一个解决方案是让所有控制器类派生自一个基类。并在基类ovveride的OnException方法内显示用户友好的错误视图,例如“〜/ Shared/Error.aspx”
您还需要在根web.config中为此解决方案定义<customErrors mode="On" >
工作。
public class BaseController : Controller
{
ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public BaseController()
{
}
protected override void OnException(ExceptionContext filterContext)
{
// Log the error that occurred.
log.Fatal("Generic Error occured",filterContext.Exception);
// Output a nice error page
if (filterContext.HttpContext.IsCustomErrorEnabled)
{
filterContext.ExceptionHandled = true;
View("Error").ExecuteResult(ControllerContext);
}
}
}
上面的解决方案捕获了大多数可能发生的“死亡错误的黄色屏幕”。
处理其他错误,如404我用如下因素图路线最后在Global.asax中的RegisterRoutes(RouteCollection路线)
// Show a 404 error page for anything else.
routes.MapRoute(
"Error",
"{*url}",
new { controller = "Shared", action = "Error" }
);
错误重定向只在我的IIS的工作不是在本地VS开发服务器。 – Santhosh