HandleError attribute doesn't have any effect

不羁的心 提交于 2019-12-08 17:20:46

问题


In my web.config I have included:

<customErrors mode="On" />

Now the yellow screen of death isn't shown anymore. I thought I'd have to include the HandleError attribute to my controller methods or the class itself:

[HandleError]
public ActionResult About()
{
    throw new Exception("Just an exception");
    return View();
}

But it doesn't have any effect, it's the same as:

public ActionResult About()
{
    throw new Exception("Just an exception");
    return View();
}

In both cases the custom error page is shown. So what is it about the HandleError attribute?


回答1:


That can happen if FilterConfig.cs, under App_Start folder of the MVC project, contains:

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

Since the HandleError filter is registered when the App starts, you don't have to decorate each controller action with this attribute.




回答2:


See the following article by Scott Gu. It has all the information about the HandleError attribute http://weblogs.asp.net/scottgu/archive/2008/07/14/asp-net-mvc-preview-4-release-part-1.aspx



来源:https://stackoverflow.com/questions/17917434/handleerror-attribute-doesnt-have-any-effect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!