1

我有基本控制器,假设它有一个名为AnyProperty

public class MyBaseController : Controller
    {
        public MyObject AnyProperty { get; set; }

        public MyBaseController()
        {
            AnyProperty = new MyObject();
        }
    }

RoleAttribute我需要使用AnyProperty. 我用

public class RoleAttribute : AuthorizeAttribute
    {
        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            MyBaseController controllerInstance = new MyBaseController();
            controllerInstance.AnyProperty   // I use like this 
        }
    }

我怎样才能得到我的AnyProperty财产 httpContext?不使用MyBaseController实例。可能吗?

4

1 回答 1

2

这是另一种方法,但它可能会有所帮助:

public override void OnAuthorization(HttpActionContext actionContext)
{
    var controller = actionContext.ControllerContext.Controller as MyBaseController;
    if (controller == null)
    {
        return;
    }
    controller.AnyProperty = new MyObject();
}
于 2013-02-19T07:35:54.750 回答