我有基本控制器,假设它有一个名为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
实例。有可能吗?
答案 0 :(得分:2)
这是另一种方法,但它可能会有所帮助:
public override void OnAuthorization(HttpActionContext actionContext)
{
var controller = actionContext.ControllerContext.Controller as MyBaseController;
if (controller == null)
{
return;
}
controller.AnyProperty = new MyObject();
}