问题
I have a basecontroller that has a property like:
public class BaseController : Controller
{
public User CurrentUser {get;set;}
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
// if session cookie found, set User object here
}
}
Now I want to create an action filer that I could set on controllers or actions that I want to do something like:
if (User.IsAdmin)
{
}
else
{
// redirect to login or some page
}
So this filter @AdminOnly
I could put on a controller or action and this will ensure that only users who have the IsAdmin flag set will be able to view the action.
Does a filter have visibility into the currently executing controller?
回答1:
Can't you use
if (filterContext.Controller is BaseController)
{
BaseController ctr = (BaseController)filterContext.Controller;
if (ctr.User.IsAdmin)
{....}
}
Link
来源:https://stackoverflow.com/questions/26023954/can-a-filter-access-properties-from-my-basecontroller