相关疑难解决方法(0)

为什么ASP.NET MVC 3异常被"处理"两次?

我使用下面的代码实现了异常处理.[编辑开始]我不知道它为什么两次调用视图.[已编辑完成]

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);
        }
    }
Run Code Online (Sandbox Code Playgroud)

HomeController的

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

        public ActionResult Exception3()
        {
            throw (new Exception());
        }
}
Run Code Online (Sandbox Code Playgroud)

错误视图(仅限共享文件夹)

@model System.Web.Mvc.HandleErrorInfo
@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <title>Error</title>
</head>
<body>
    <h2>
        Sorry, an error occurred while …
Run Code Online (Sandbox Code Playgroud)

c# exception-handling asp.net-mvc-3

3
推荐指数
1
解决办法
3437
查看次数

标签 统计

asp.net-mvc-3 ×1

c# ×1

exception-handling ×1