OnActionExecuting
会在某项操作即将执行时触发。
如果我的动作有actionfilter
[myCustomActionFilter]
public ActionResult MyAction()
{
//implementation
}
是否可以确定(OnActionExecuting
事件内)某个操作已应用myCustomActionFilter
?
答案 0 :(得分:2)
上述方法区分大小写。
public void OnActionExecuting(ActionExecutingContext filterContext)
{
var actionDescriptor = filterContext.ActionDescriptor;
if (actionDescriptor.IsDefined(typeof(myCustonActionFilter), true))
{
var attributeInstance = (myCustomActionFilter) actionDescriptor.GetCustomAttributes(typeof(myCustomActionFilter), true);
}
}
答案 1 :(得分:1)
public void OnActionExecuting(ActionExecutingContext filterContext)
{
var controllerType = filterContext.Controller.GetType();
var actionMethod = controllerType.GetMethod(filterContext.ActionDescriptor.ActionName);
if (actionMethod.IsDefined(typeof(myCustomActionFilter),true))
{
var attributeInstance =
(myCustomActionFilter) actionMethod.GetCustomAttributes(typeof (myCustomAct
ionFilter), true);
}