属性似乎根本没有作用 [英] attribute does not seem to act at all
问题描述
我在使用控制器操作上的 [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:
- 检查您的语法.
- 确保您正在编辑项目根目录中的
Web.config
,而不是视图中的那个. - 确保没有代码集
HttpContext.IsCustomErrorEnabled
. - 如果一切都失败了,请尝试在
Web.config
中关闭调试
- Check your syntax.
- Make sure you're editing the
Web.config
in the project root, not the one in Views. - Make sure no code sets
HttpContext.IsCustomErrorEnabled
. - 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屋!