ninject / Ninject.Web.Mvc

extension for ninject providing integration with ASP.NET MVC
http://ninject.org/
Other
238 stars 85 forks source link

Controller level filter stops global filter from executing (MVC3, 2.2+) #18

Closed pleb closed 13 years ago

pleb commented 13 years ago

A global filter is not executing when a controller contains a controller level filter.

Tried with ninject 2.3.0.6 and 2.2.1.1

To reproduce

    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
    public class AlphaAttribute : Attribute {}

    public class FilterAlpha : IActionFilter
    {
        public void OnActionExecuting(ActionExecutingContext filterContext) {}

        public void OnActionExecuted(ActionExecutedContext filterContext)
        {
            filterContext.Controller.ViewData["alpha"] = "here";
        }
    }

    public class FilterBeta : IActionFilter
    {
        public void OnActionExecuting(ActionExecutingContext filterContext) {}

        public void OnActionExecuted(ActionExecutedContext filterContext)
        {
            filterContext.Controller.ViewData["beta"] = "here";
        }
    }

    [Alpha]
    public class WithController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
    }

    public class WithoutController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
    }

Registration

            kernel.BindFilter<FilterAlpha>(FilterScope.Controller, 0)
                .WhenControllerHas<AlphaAttribute>();

            kernel.BindFilter<FilterBeta>(FilterScope.Global, 0);

Views

<p>Alpha? @ViewData["alpha"]</p>
<p>Beta? @ViewData["beta"]</p>

Example project can be found here

remogloor commented 13 years ago

Replaced by https://github.com/ninject/ninject/issues/36