Do the MVC Action Filter Attributes run, before the controller is instantiated? I have a property of the controller, that I would like to check from the ActionFilter. Is this possible?
views:
183answers:
3
+3
A:
According to the Professional ASP.NET MVC 1.0 book, ActionFilters run after the controller is instantiated. By the time of OnActionExecuting (the first method called by an ActionFilter), the Controller context is available.
Ender
2009-05-28 21:44:12
+2
A:
The Controller will get instantiated before the Action Filter's OnActionExecuted and OnActionExecuting events are fired. Also you can access the Controller through the "filterContext" parameter that's passed to the event handlers.
public class TestActionAttribute : FilterAttribute, IActionFilter
{
#region IActionFilter Members
public void OnActionExecuted(ActionExecutedContext filterContext)
{
var controller = filterContext.Controller;
}
public void OnActionExecuting(ActionExecutingContext filterContext)
{
var controller = filterContext.Controller;
}
#endregion
}
Chris Pietschmann
2009-05-28 21:44:36
+1
A:
Abstract class System.Web.Mvc.ActionFilterAttribute (derive your own ActionFilter from this class) have 4 OnXXX methods:
- OnActionExecuting
- OnActionExecuted
- OnResultExecuting
- OnResultExecuted
I think in OnActionExecuting you can check your controller:
YourController controller = filterContext.Controller as YourController
if(controller != null)
{
// check your controller
}
eu-ge-ne
2009-05-28 21:44:44