Ada*_*lin 5 c# asp.net-web-api
我有一个习惯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);
}
}
Run Code Online (Sandbox Code Playgroud)
然后我的控制器用CustomActionFilter.我正在寻找的是一种方式(在我的控制器方法中)做类似的事情:
[CustomActionFilter]
public class MyController : ApiController {
public ActionResult MyAction() {
//How do I get the 'success' from my attribute?
}
}
Run Code Online (Sandbox Code Playgroud)
如果有更可接受的方式,请告诉我.
我发现我可以做以下事情来解决我的问题:
[CustomActionFilter]
public class MyController : ApiController {
public ActionResult MyAction() {
var myAttribute = ControllerContext
.ControllerDescriptor
.GetCustomAttributes<CustomActionFilter>()
.Single();
var success = myAttribute.success;
}
}
Run Code Online (Sandbox Code Playgroud)