为什么ASP.NET MVC 3的例外是'处理'两次? [英] Why are ASP.NET MVC 3 exceptions being 'handled' twice?

查看:187
本文介绍了为什么ASP.NET MVC 3的例外是'处理'两次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用下面code。我不知道为什么它在调用视图两次实施异常处理。

I have implemented exception handling using below code. I do not know why it is calling view twice.

Basecontroller

Basecontroller

public class BaseController : Controller
    {
        protected override void OnException(ExceptionContext filterContext)
        {
            if (filterContext.HttpContext.IsCustomErrorEnabled)
            {
                filterContext.ExceptionHandled = true;

                if (filterContext.Exception.GetType() == typeof(ArgumentOutOfRangeException))
                {
                    this.View("OutOfRange").ExecuteResult(this.ControllerContext);
                }
                else
                {
                    this.View("Error").ExecuteResult(this.ControllerContext);
                }
            }

            base.OnException(filterContext);
        }
    }

HomeController的

HomeController

public class HomeController : BaseController
{
        public ActionResult Exception2()
        {
            throw (new ArgumentOutOfRangeException());
        }

        public ActionResult Exception3()
        {
            throw (new Exception());
        }
}

错误查看(只共享文件夹)

Error View (Shared folder only)

@model System.Web.Mvc.HandleErrorInfo
@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <title>Error</title>
</head>
<body>
    <h2>
        Sorry, an error occurred while processing your request.
    </h2>
    <h3>
        @if (Model != null)
        {
            <p>@Model.Exception.GetType().Name<br />
                thrown in @Model.ControllerName @Model.ActionName</p>
            <br />
            @Model.Exception
        }
    </h3>
</body>
</html>

OutOfRange视图(仅适用于共享文件夹)

OutOfRange view (Shared folder only)

@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <title>OutOfRange Exception</title>
</head>
<body>
    <div>
        <h3>
            @if (Model != null)
            {
                <p>@Model.Exception.GetType().Name<br />
                    thrown in @Model.ControllerName @Model.ActionName</p>
                <br />
                @Model.Exception
            }
        </h3>
    </div>
</body>
</html>

执行网址:的http:// [域名] /主页/ Exception2

这是工作的罚款。

执行网址:的http:// [域名] /主页/ Exception3

这是不工作的罚款。你可以看到对不起,发生了错误处理您的请求。来了两次。当我调试使用上面的网址,被称为Error视图申请两次(第一次型号为NULL和第二次模型包含了一定的价值)。我想知道什么是错我的执行?

Here it is not working fine. You can see that "Sorry, an error occurred while processing your request." is coming twice. When I debugged the application using above URL, Error view called twice ( First time Model is null and second time Model contains some value). May I know what is wrong with my implementation?

推荐答案

您可以尝试:


  1. 删除默认的HandleError 全球行动过滤器从的Global.asax 如果是present属性。当你创建一个新的ASP.NET MVC 3应用程序的默认模板注册到 RegisterGlobalFilters 方法内。所以注释掉注册这个属性就行了。

  2. 在你的web.config启用自定义错误:

  1. Remove the default HandleError global action filter attribute from your Global.asax if it is present. When you create a new ASP.NET MVC 3 application the default template registers it inside the RegisterGlobalFilters method. So comment out the line which registers this attribute.
  2. Enable custom errors in your web.config:

<customErrors mode="On" />


更新:

如果你想传递一个模式向 Error.cshtml 视图(因为它是强类型为 HandleErrorInfo ),你可以做到以下几点:

If you want to pass a model to the Error.cshtml view (as it is strongly typed to HandleErrorInfo) you could do the following:

else
{
    string controllerName = filterContext.RouteData.GetRequiredString("controller");
    string actionName = filterContext.RouteData.GetRequiredString("action");
    var model = new HandleErrorInfo(filterContext.Exception, controllerName, actionName);
    var result = new ViewResult
    {
        ViewName = "Error",
        ViewData = new ViewDataDictionary<HandleErrorInfo>(model)
    };
    filterContext.Result = result;
}

这篇关于为什么ASP.NET MVC 3的例外是'处理'两次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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