过滤器可以从我的BaseController访问属性吗? [英] Can a filter access properties from my BaseController?
本文介绍了过滤器可以从我的BaseController访问属性吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个basecontroller,其属性类似于:
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
}
因此该过滤器@AdminOnly
我可以放置控制器或操作,这将确保只有设置了IsAdmin标志的用户才能查看该操作.
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?
推荐答案
您不能使用
if (filterContext.Controller is BaseController)
{
BaseController ctr = (BaseController)filterContext.Controller;
if (ctr.User.IsAdmin)
{....}
}
这篇关于过滤器可以从我的BaseController访问属性吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文