[英]How can I access a property from an ActionFilterAttribute in my ApiController?
我有一個自定義的ActionFilterAttribute
。 為了這個問題,讓我們假設它如下:
public class CustomActionFilterAttribute : ActionFilterAttribute {
public bool success { get; private set };
public override void OnActionExecuting(HttpActionContext actionContext) {
//Do something and set success
success = DoSomething(actionContext);
}
}
然后我的控制器使用CustomActionFilter
進行修飾。 我正在尋找的是一種方式(在我的控制器方法中)做類似的事情:
[CustomActionFilter]
public class MyController : ApiController {
public ActionResult MyAction() {
//How do I get the 'success' from my attribute?
}
}
如果有更可接受的方式,請告訴我。
我發現我可以做以下事情來解決我的問題:
[CustomActionFilter]
public class MyController : ApiController {
public ActionResult MyAction() {
var myAttribute = ControllerContext
.ControllerDescriptor
.GetCustomAttributes<CustomActionFilter>()
.Single();
var success = myAttribute.success;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.