0

I have created an ActionFilterAttribute called LayoutAttribute and added it to the ActionResult:

Controller:

    [Layout(PageType.Department,"dnr")]
    public ActionResult Kd(string mainBody, int dnr)
    {

LayoutAttribute:

using System.Web.Mvc;
using Komplett.Infrastructure.NInject;
using Ninject;


namespace Minion.Services.PageState
{
  public class LayoutAttribute : ActionFilterAttribute
  {
    private readonly PageType _pageType;
    private readonly string _pageIdName;

    public LayoutAttribute(PageType PageType,string PageIdName = "")
    {
        _pageType = PageType;
        _pageIdName = PageIdName;
    }

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        var pageStateModel = KernelContainer.Kernel.Get<PageStateModel>();
        if (filterContext.ActionParameters.ContainsKey(_pageIdName ?? ""))
        {
            pageStateModel.PageId = (string)filterContext.ActionParameters[_pageIdName];
        }
        pageStateModel.PageType = _pageType;
    }
}

}

The problem is that OnActionExecuting is never called. I also tried to register the attribute in global.asax.cs, but then I needed a parameterless constructor. I created this and added it to global.asax.cs like this:

GlobalFilters.Filters.Add(new LayoutAttribute());

This, without any luck.

What am I doing wrong? How can I make MVC call OnActionExecuting?

Numm3n
  • 1,021
  • 2
  • 9
  • 26
  • You should not need to register it in the Global Filters. Are you sure there are not any other filters intervening before yours and redirecting the page? – Tallmaris Jul 25 '14 at 09:47
  • Use properties, like shown here http://stackoverflow.com/questions/4348071/how-to-pass-parameters-to-a-custom-actionfilter-in-asp-net-mvc-2. Most likely a non-default constructor is not supported. – Marcel N. Jul 25 '14 at 10:10
  • What happens if you put it on controller ? – Ajay Kelkar Jul 25 '14 at 13:58

1 Answers1

0

Make sure you're implementing

System.Web.Mvc.ActionFilterAttribute

and not

System.Web.Http.Filters.ActionFilterAttribute

They both have OnActionExecuting and OnActionExecuted Methods, so it can be a little deceiving

Ajay Kelkar
  • 4,591
  • 4
  • 30
  • 29