2013-07-29 59 views
7

在我的web.config我已经包括了:现在死亡的黄色屏幕不再显示的HandleError属性没有任何效果

<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属性是什么呢?

回答

13

可能发生,如果FilterConfig.cs,在MVC项目的App_Start文件夹下,包含:

public static void RegisterGlobalFilters(GlobalFilterCollection filters) 
{ 
    filters.Add(new HandleErrorAttribute()); 
} 

由于应用程序启动时的HandleError过滤器被注册,您不必使用此属性来修饰每个控制器操作。

+1

一个优秀和简洁的解释,也回答了这个问题:“RegisterGlobalFilters'的目的是什么? – Dan