ASP.NET MVC 句柄错误 [英] ASP.NET MVC HandleError

查看:34
本文介绍了ASP.NET MVC 句柄错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何处理 asp.net MVC Preview 5 中的 [HandleError] 过滤器?
我在我的 Web.config 文件中设置了 customErrors

How do I go about the [HandleError] filter in asp.net MVC Preview 5?
I set the customErrors in my Web.config file

<customErrors mode="On" defaultRedirect="Error.aspx">
  <error statusCode="403" redirect="NoAccess.htm"/>
  <error statusCode="404" redirect="FileNotFound.htm"/>
</customErrors>

并像这样将 [HandleError] 放在我的控制器类之上:

and put [HandleError] above my Controller Class like this:

[HandleError]
public class DSWebsiteController: Controller
{
    [snip]
    public ActionResult CrashTest()
    {
        throw new Exception("Oh Noes!");
    }
}

然后我让我的控制器继承这个类并对它们调用 CrashTest().Visual Studio 在错误处停止,按 f5 继续后,我被重新路由到 Error.aspx?aspxerrorpath=/sxi.mvc/CrashTest(其中 sxi 是所用控制器的名称.当然找不到路径,我收到'/'应用程序中的服务器错误."404.

Then I let my controllers inherit from this class and call CrashTest() on them. Visual studio halts at the error and after pressing f5 to continue, I get rerouted to Error.aspx?aspxerrorpath=/sxi.mvc/CrashTest (where sxi is the name of the used controller. Off course the path cannot be found and I get "Server Error in '/' Application." 404.

此站点已从预览版 3 移植到预览版 5.除了错误处理之外,一切都在运行(移植的工作量不大).当我创建一个完整的新项目时,错误处理似乎有效.

This site was ported from preview 3 to 5. Everything runs (wasn't that much work to port) except the error handling. When I create a complete new project the error handling seems to work.

想法?

--注意--
由于这个问题现在有超过 3K 的视图,我认为放入我目前使用的内容(ASP.NET MVC 1.0)会有所帮助.在 mvc contrib 项目 中有一个很棒的属性叫做RescueAttribute"你也应该检查一下;)

--Note--
Since this question has over 3K views now, I thought it would be beneficial to put in what I'm currently (ASP.NET MVC 1.0) using. In the mvc contrib project there is a brilliant attribute called "RescueAttribute" You should probably check it out too ;)

推荐答案

[HandleError]

当你只为你的类(或你的操作方法)提供 HandleError 属性时,当发生未处理的异常时,MVC 将首先在控制器的视图文件夹中查找名为错误"的相应视图.如果在那里找不到它,那么它将继续在共享视图文件夹中查找(默认情况下应该有一个 Error.aspx 文件)

When you provide only the HandleError attribute to your class (or to your action method for that matter), then when an unhandled exception occurs MVC will look for a corresponding View named "Error" first in the Controller's View folder. If it can't find it there then it will proceed to look in the Shared View folder (which should have an Error.aspx file in it by default)

[HandleError(ExceptionType = typeof(SqlException), View = "DatabaseError")]
[HandleError(ExceptionType = typeof(NullReferenceException), View = "LameErrorHandling")]

您还可以使用有关您要查找的异常类型的特定信息来堆叠其他属性.此时,您可以将错误定向到默认错误"视图以外的特定视图.

You can also stack up additional attributes with specific information about the type of exception you are looking for. At that point, you can direct the Error to a specific view other than the default "Error" view.

欲了解更多信息,请查看 Scott Guthrie 的博文 关于它.

For more information, take a look at Scott Guthrie's blog post about it.

这篇关于ASP.NET MVC 句柄错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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