异常过滤器属性未执行

时间:2015-12-19 05:35:29

标签: c# asp.net-mvc exception-handling

我正在关注ASP.NET MVC 5中的异常过滤器属性教程。 以下是我的代码:

/Infrastructure/RangeExceptionAttribute.cs

using System;
using System.Web.Mvc;

namespace Filters.Infrastructure
{
    public class RangeExceptionAttribute : FilterAttribute, IExceptionFilter
    {
        public void OnException(ExceptionContext filterContext)
        {
            if (!filterContext.ExceptionHandled && filterContext.Exception is ArgumentOutOfRangeException)
            {
                filterContext.Result = new RedirectResult("~/Content/RangeErrorPage.html");
                filterContext.ExceptionHandled = true;
            }
        }
    }
}

/Content/RangeErrorPage.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Range Error</title>
</head>
<body>
<h2>Sorry</h2>
<span>One of the arguments was out of the expected range.</span>
</body>
</html>

/Controllers/HomeController.cs

using System.Web.Mvc;
using Filters.Infrastructure;
using System;
using System.Web;

namespace Filters.Controllers
{
    public class HomeController : Controller
    {
        [RangeException]
        public string RangeTest(int id)
        {
            if (id > 100)
                return String.Format("The id value is: {0}", id);
            else
                throw new ArgumentOutOfRangeException("id", id, "");
        }
    }
}

现在,在导航到网址/Home/RangeTest/50后,期望用户将被重定向到/Content/RangeErrorPage.html页面。但是,Visual Studio调试器中断并说ArgumentOutOfRangeException was unhandled by user code。这可能是什么问题?我错过了什么吗?

0 个答案:

没有答案