ASP.NET MVC HandleError 视图未找到
我正在尝试使用 HandleError 属性在 ASP.NET MVC3 中实现异常处理。
我使用的代码如下所示:
[HandleError(Order = 1, ExceptionType = typeof(SocketsOfflineException), View="EndSystemDownError")]
当 EndSystemDownError 视图位于“Shared”文件夹中时,它会按预期工作。但是,我有许多错误视图,我想将它们单独放入一个专门用于错误的文件夹中,称为“错误”。
因此,我将 EndSystemDownError 视图从共享文件夹中拉出并放入 Views 文件夹中的“Error”子文件夹中。然后,我将 View 属性更新为 ~/Views/Error/EndSystemDownError
。然而,这会导致异常,指出未找到该视图。我也尝试了 /Views/Error/EndSystemDownError
,结果相同。
我尝试添加带有 EndSystemDownError
操作的 ErrorController
,既作为故障排除措施,又因为我想向视图添加一些控制器功能。这没有效果。
我似乎无法弄清楚我做错了什么。也许这是 MVC 中的一个错误,或者它可能不支持共享文件夹之外的错误视图,这将令人失望。
I'm trying to implement exception handling in ASP.NET MVC3 using the HandleError attribute.
The code that I'm using looks like this:
[HandleError(Order = 1, ExceptionType = typeof(SocketsOfflineException), View="EndSystemDownError")]
This works as expected when the EndSystemDownError view is located in the "Shared" folder. However, I have a number of Error views, and I want to separate them out into a folder specifically for Errors, called "Error".
So I pull the EndSystemDownError view out of the shared folder and into an "Error" subfolder in the Views folder. I then update the View property to ~/Views/Error/EndSystemDownError
. This however results in an exception stating that the view was not found. I tried /Views/Error/EndSystemDownError
as well, with the same results.
I have tried adding an ErrorController
with an EndSystemDownError
action, both as a troubleshooting measure, and because I would like to add some controller functionality to the view. This has no effect.
I can't seem to figure out what I'm doing wrong. Perhaps this is a bug in MVC, or maybe it doesn't support error views outside of the Shared folder, which would be disappointing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
AFAIK 您无法修改这些视图的位置。它们应该位于
~/Views/Shared
中。您可以指定每个异常类型的名称:其中
EndSystemDownError
视图位于~/Views/Shared/EndSystemDownError.aspx
中。AFAIK you cannot modify the location of those views. They should be in
~/Views/Shared
. You could specify the name though per exception type:where the
EndSystemDownError
view is located in~/Views/Shared/EndSystemDownError.aspx
.如果将错误子文件夹放在共享文件夹中,则可以通过 Errors/whatevererror.aspx 引用它,应该没问题。
我经常这样做。毕竟他们是共同的观点。 :)
If you put your errors subfolder inside of the shared folder, you can refer to it by Errors/whatevererror.aspx and it should be fine.
I do this frequently. After all they are shared views. :)
你试过吗
did you try
Orchard 模块编程的旁注:
HandleError
属性在 Orchard 模块中不起作用,因为 Orchard 实现了自定义过滤器处理本身。您需要实现一个
FilterProvider
来重写OnResultExecuted()
以捕获视图中引发的异常。参见
src\Orchard\Exceptions\Filters\UnhandledExceptionFilter.cs
,它实现了常见的Orchard错误页面。Side note for Orchard module programming:
The
HandleError
attribute won't work in Orchard modules as Orchard implements a custom filter handling itself.You need to implement a
FilterProvider
that overrides theOnResultExecuted()
to catch exceptions thrown in a view.See
src\Orchard\Exceptions\Filters\UnhandledExceptionFilter.cs
, it implements the common Orchard error page.