Asp.Net MVC 3 应用程序 HandleError 不起作用
好吧,我想我已经准备好了 StackOverflow 上关于此的所有帖子,但仍然没有成功。
我已在 global.asax 中确认 HandleErrorAttribute 过滤器正在注册。 我已在 web.config 中设置 customErrors mode="On"
。 我已在 Error.cshtml 文件中将 Layout=null
更改为 Layout="~/Views/Shared/_Layout.cshtml"
。
然后,我在帐户控制器的 Index 操作中添加了一个 throw Exception()
并浏览到 localhost:5050/Account/Index
,但最终得到的是标准 YSOD 错误堆栈内容。
我什至尝试过以下操作(尽管根据我的理解,这并不重要):
- 将
[HandleError]
和[HandleError(View="Error")]
添加到我的控制器。 - 将
defaultRedirect="Error"
添加到 web.config 中的 customErrors
我还能做什么?有没有办法告诉 HandleError 过滤器中发生了什么,或者告诉是否正在生成错误视图?
感谢您的帮助。
PS顺便说一下,我使用的是VS2010,IE9 &铬合金
Okay, I think I've ready every post on StackOverflow regarding this and still no luck.
I've confirmed in my global.asax that the HandleErrorAttribute filter is being registered.
I've set customErrors mode="On"
in my web.config.
I've changed Layout=null
to Layout="~/Views/Shared/_Layout.cshtml"
in my Error.cshtml file.
I then added a throw Exception()
in my Account controller's Index action and browse to localhost:5050/Account/Index
but I wind up with the standard YSOD error stack stuff.
I've even tried the following (although from my understanding it shouldn't matter):
- Add
[HandleError]
and[HandleError(View="Error")]
to my controller. - Add
defaultRedirect="Error"
to my customErrors in web.config
What else can I do? Is there a way to tell what's going on in the HandleError filter or to tell if the Error View is being generated at all?
Thanks for your help.
P.S. Incidentally, I'm using VS2010, IE9 & Chrome
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我无法重现该问题。您的
_Layout.cshtml
或Error.cshtml
模板中很可能存在错误。或者在这些模板可能依赖的某些部分。步骤:
添加到 web.configthrow new Exception(); 位于
Account
控制器上的LogOn
操作主体中。/account/logon
Error.cshtml
视图已正确呈现您获得的 YSOD 通常包含一个异常堆栈跟踪,可以帮助您隔离问题。因此,要尝试隔离问题,请尝试消除这些模板中可能导致问题的任何潜在代码。尝试将
Layout = null
放入错误模板中以消除布局。如果您的布局或错误模板中存在错误(正如我怀疑的那样),您最终会得到以下结果:如果您仍然无法隔离问题,请尝试订阅
Global.asax
中的Application_Error
事件,看看是否可以获得有关异常的更多信息。I cannot reproduce the problem. It is very likely that there's an error either in your
_Layout.cshtml
orError.cshtml
templates. Or in some partial that those templates might be depending upon.Steps:
<customErrors mode="On" />
to web.configthrow new Exception();
in the body of theLogOn
action on theAccount
controller./account/logon
Error.cshtml
view is correctly renderedThe YSOD you got normally contains an exception stacktrace that should help you isolate the problem. So to try to isolate the problem, try eliminating any potential code in those templates that might cause the problem. Try putting
Layout = null
in the Error template to eliminate the layout. And if there's an error (as I suspect) in either your Layout or Error templates you will end up with this:If you still are unable to isolate the problem try subscribing to the
Application_Error
event in yourGlobal.asax
and see if you can get more information about the exception.好吧,无知并不总是幸福……有时无知简直令人沮丧!
事实证明,我正在编辑位于 Views 文件夹中的 web.config,而不是像我想象的那样编辑我的网站的 web.config。
将
customErrors mode="On"
添加到我的 real web.config 后,一切都很好。感谢所有试图提供帮助的人。
Okay, ignorance is not always bliss ... sometimes being ignorant is just plain frustrating!
It turns out I was editing the web.config located in my Views folder instead of the web.config for my site like I thought I was.
Once I added
customErrors mode="On"
to my real web.config, things worked great.Thanks to everyone that tried to help.