为什么ASP.NET MVC 3异常被“处理”两次? [英] Why are ASP.NET MVC 3 exceptions being 'handled' twice?

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

问题描述

我使用下面的代码实现了异常处理。我不知道为什么调用视图两次。

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>

执行URL: http:// [domainName] / Home / Exception2

这里工作正常。

执行URL: http:// [domainName] / Home / Exception3

这里不正常。您可以看到对不起,处理您的请求时发生错误。来了两次当我使用上面的URL调试应用程序时,错误视图被调用两次(第一次模型为null,第二次模型包含一些值)。我可以知道我的实现有什么问题吗?

推荐答案

事情要尝试:


  1. 删除如果存在,则从 Global.asax 中的默认 HandleError 全局操作过滤器属性。当您创建一个新的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" />







更新:


UPDATE:

如果要将模型传递给 Error.cshtml 视图(因为它很强您可以执行以下操作:

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天全站免登陆