2

I'm following a tutorial on Exception Filter Attributes in ASP.NET MVC 5. The following is my code:

/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, "");
        }
    }
}

Now upon navigation to URL /Home/RangeTest/50, the expectation is user would be redirected to the /Content/RangeErrorPage.html page. However, Visual Studio debugger breaks and says ArgumentOutOfRangeException was unhandled by user code. What could be the issue here? Am I missing something?

Bat_Programmer
  • 6,717
  • 10
  • 56
  • 67

0 Answers0