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?