In my MVC3 C# project, I have been looking at ways to construct a navigation menu in controller code and pass that to the master page. (i want to initialize the menu in controller so I can do some authorization and roles checking, etc…)
So far, I found several answers on here taht all seem to require overriding the OnActionExecuted method of a BaseController (from which all other controllers extend)
Example 1(see accepted answer): How to create a strongly typed master page using a base controller in ASP.NET MVC
Example 2: "Security aware" action link?
But wy not just put that same code in the Base Controller’s constructor?
If all your controllers inherit from the same base controller, there is no reason why you could not do it this way.
The advantage of an
ActionFilterAttributeis that it allows you to insert your logic on any controller by simply decorating it with the attribute, allowing you the freedom of inheriting from any base class you choose.