Asp.Net MVC 3 应用程序 HandleError 不起作用

发布于 2025-01-08 13:25:19 字数 744 浏览 0 评论 0原文

好吧,我想我已经准备好了 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 错误堆栈内容。

我什至尝试过以下操作(尽管根据我的理解,这并不重要):

  1. [HandleError][HandleError(View="Error")] 添加到我的控制器。
  2. 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):

  1. Add [HandleError] and [HandleError(View="Error")] to my controller.
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

み格子的夏天 2025-01-15 13:25:19

我无法重现该问题。您的 _Layout.cshtmlError.cshtml 模板中很可能存在错误。或者在这些模板可能依赖的某些部分。

步骤:

  1. 使用默认模板创建新的 ASP.NET MVC 3 应用程序
  2. 添加到 web.config
  3. 添加 throw new Exception(); 位于 Account 控制器上的 LogOn 操作主体中。
  4. 运行站点并导航到 /account/logon
  5. 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 or Error.cshtml templates. Or in some partial that those templates might be depending upon.

Steps:

  1. Create a new ASP.NET MVC 3 application using the default template
  2. Add <customErrors mode="On" /> to web.config
  3. Add throw new Exception(); in the body of the LogOn action on the Account controller.
  4. Run the site and navigate to /account/logon
  5. The Error.cshtml view is correctly rendered

The 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:

enter image description here

If you still are unable to isolate the problem try subscribing to the Application_Error event in your Global.asax and see if you can get more information about the exception.

剑心龙吟 2025-01-15 13:25:19

好吧,无知并不总是幸福……有时无知简直令人沮丧!

事实证明,我正在编辑位于 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文