属性似乎根本不起作用

Tom*_*han 11 error-handling asp.net-mvc attributes handleerror

我在控制器操作上使用[HandleError]属性时遇到问题 - 它似乎根本不起作用(即过滤器是否存在并不重要 - 我得到相同的结果......).当抛出异常时,我在'/'应用程序错误页面而不是我的自定义视图中得到标准的红色服务器错误.

我在SO上找到了关于这个主题的几个其他线程,在大多数情况下,似乎在web.config中将customErrors选项设置为On解决了这个问题.它不适合我,所以我需要找到一个不同的解决方案.

我的控制器动作:

[HandleError]
public ActionResult Index()
{
    throw new Exception("oops...");
    return View();
}
Run Code Online (Sandbox Code Playgroud)

在我的web.config文件中

<customErrors mode="On"></customErrors>
Run Code Online (Sandbox Code Playgroud)

我确保Error.aspx文件也在共享目录中.我错过了什么?

我正在运行ASP.NET MVC RC Refresh.

Cra*_*ntz 13

要了解两件有用的事情:

默认情况下,HandleError在开发服务器下运行时不执行任何操作.目的是向开发人员展示更多有用的信息:

public virtual void OnException(ExceptionContext filterContext) {
    if (filterContext == null) {
        throw new ArgumentNullException("filterContext");
    }

    // If custom errors are disabled, we need to let the normal ASP.NET
    // exception handler execute so that the user can see useful
    // debugging information.
    if (filterContext.ExceptionHandled
        || ! filterContext.HttpContext.IsCustomErrorEnabled) {
        return;
    }
Run Code Online (Sandbox Code Playgroud)

请注意,这种情况正是customError应该控制的.如果设置customError="On"不会更改此行为:

  1. 检查你的语法.
  2. 确保您Web.config在项目根目录中编辑,而不是在视图中编辑.
  3. 确保没有代码集HttpContext.IsCustomErrorEnabled.
  4. 如果所有其他方法都失败了,请尝试关闭调试 Web.config

其次,某些类型的错误HandleError永远不会处理,特别是ASP.NET编译错误.您没有说出您遇到的错误.