Reputation: 2924
I have a controller which inherits from a BaseController class. This BaseController has a protected property currentUser which is of type myUser.
I have created a "Custom Action Filter Attribute" and I need to access the value of myUser within this atrtribute's OnActionExecuting() event.
Is this possible? If so, how can I implement this feature?
Regards.
Upvotes: 1
Views: 87
Reputation: 1397
Yes you can do it but first you have convert your currentUser property from protected to public or expose it through read only property or method.
Then you can access it using the following
var baseController = filterContext.Controller as BaseController;
if (baseController != null) {
//Access your exposed **public** property or method
baseController.currentUser
}
Upvotes: 2
Reputation: 2047
You have to overide the OnActionExecuting method which will have an 'ActionExecutingContext' object passed in.
That object has a property Controller, which is the current controller. You can check if that is of the desired type.
So:
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
MyUser currentUser = null;
var controller = filterContext.Controller as BaseController;
if (controller != null) {
currentUser = controller.CurrentUser;
}
}
Upvotes: 2