属性似乎根本不起作用 [英] attribute does not seem to act at all

查看:22
本文介绍了属性似乎根本不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am having problems using the [HandleError] attribute on my Controller Actions - it doesn't seem to work at all (i.e. it doesn't matter if the filter is there or not - I get the same results...). When an Exception is thrown, I get the standard red-toned Server Error in '/' Application error page instead of my custom view.

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

I have found a couple of other threads on the subject here on SO, and in most cases it seems that setting the customErrors option to On in web.config solved the problem. It has not for me, so I need to find a different solution.

我的控制器操作:

[HandleError]
public ActionResult Index()
{
    throw new Exception("oops...");
    return View();
}

在我的 web.config 文件中

In my web.config file

<customErrors mode="On"></customErrors>

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

I have made sure that the Error.aspx file is in the Shared directory, too. What am I missing?

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

I am running ASP.NET MVC RC Refresh.

推荐答案

要知道的两件事:

默认情况下,HandleError 在开发服务器下运行时什么都不做.目的是向开发者展示更多有用的信息:

By default, HandleError does nothing when running under the development server. The intention is to show developers more useful information:

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;
    }

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

Note that this case is precisely what customError is supposed to control. If setting customError="On" does not change this behavior:

  1. 检查您的语法.
  2. 确保您正在编辑项目根目录中的 Web.config,而不是视图中的那个.
  3. 确保没有代码集HttpContext.IsCustomErrorEnabled.
  4. 如果一切都失败了,请尝试在 Web.config
  5. 中关闭调试
  1. Check your syntax.
  2. Make sure you're editing the Web.config in the project root, not the one in Views.
  3. Make sure no code sets HttpContext.IsCustomErrorEnabled.
  4. If all else fails, try turning debug off in Web.config

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

Second, there certain types of errors which HandleError will never handle, notably ASP.NET compilation errors. You don't say which error you're encountering.

这篇关于属性似乎根本不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆