在我的web.config中,我包括:
<customErrors mode="On" />
现在黄色的死亡屏幕不再显示了.
我以为我必须将HandleError属性包含在我的控制器方法或类本身中:
[HandleError] public ActionResult About() { throw new Exception("Just an exception"); return View(); }
但它没有任何影响,它与以下相同:
public ActionResult About() { throw new Exception("Just an exception"); return View(); }
在这两种情况下都会显示自定义错误页面.那么HandleError属性又是什么呢?
解决方法
如果在MVC项目的App_Start文件夹下的FilterConfig.cs包含以下内容,则会发生这种情况:
public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new HandleErrorAttribute()); }
由于在应用程序启动时注册了HandleError过滤器,因此您不必使用此属性修饰每个控制器操作.