ASP.NET MVC的HandleError [英] ASP.NET MVC HandleError

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

问题描述

我如何去在asp.net MVC preVIEW的 [的HandleError] 过滤器5?结果
我设置的customErrors在我的Web.config文件

 <的customErrors模式=ON的defaultRedirect =Error.aspx>
  <错误状态code =403重定向=NoAccess.htm/>
  <错误状态code =404重定向=FileNotFound.htm/>
< /&的customErrors GT;

和像这样把[的HandleError]我上面的控制器类:

  [的HandleError]
公共类DSWebsiteController:控制器
{
    [SNIP]
    公众的ActionResult撞击测试()
    {
        抛出新的异常(糟糕!);
    }
}

然后,我让我的控制器,从该类继承并调用撞击测试()在他们身上。
Visual Studio中停止在错误和pressing F5后继续,我得到重新路由到Error.aspx?aspxerrorpath = / sxi.mvc /撞击测试(其中SXI是使用控制器的名称。
当然,路径无法找到,我得到在'/'Application Server的错误。 404。

这个网站是从preVIEW 3移植到5。
一切运行(是没有那么多的工作端口),除了错误处理。
当我创建一个完整的新项目中的错误处理似乎工作。

想法?

- 注 - 结果
因为现在这个问题已超过3K的意见,我认为这将是有益的,放在什么我目前用(ASP.NET MVC 1.0)。
MVC的contrib项目有一个名为RescueAttribute的辉煌属性
你或许应该看看太;)


解决方案

  [的HandleError]

当你到你的类只提供的HandleError属性(或者为此事你的操作方法),那么当出现未处理的异常MVC将查找名为在控制器的视图文件夹错误第一相应的视图。如果它不能找到它存在那么它会继续在共享文件夹视图看(这应该在默认情况下它的Error.aspx文件)

  [的HandleError(ExceptionType = typeof运算(SQLEXCEPTION),查看=DatabaseError)]
[的HandleError(ExceptionType = typeof运算(NullReferenceException异常),查看=LameErrorHandling)]

您还可以堆叠有关的异常你正在寻找特定类型信息的附加属性。在这一点上,你可以直接向错误比默认的错误的看法等特定视图。

有关详细信息,看一看的斯科特Guthrie的博客文章一下吧。

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>

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

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

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.

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.

Ideas?

--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]

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.

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

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

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