我有2个动作过滤器,我需要一个在另一个之前显式执行。我没有任何运气让他们按照预期的顺序合作和执行。
public class RedirectSingleAttribute : ActionFilterAttrbute {
public UrlHelper Url { get; set; } // <= injected in OnActivating
public override void OnActionExecuted(ActionExectuedContext filterContext) {
/* ... magic ... */
}
}
public class JsonRedirectAttribute : ActionFilterAttribute {
public override void OnActionExecuted(ActionExecutedContext filterContext) {
/* ... magic ... */
}
}
public class SearchController : Controller {
[RedirectSingle(Order = 1)]
[JsonRedirect(Order = 2)]
public ActionResult Query(SearchCriteria criteria) {
/* some code */
}
}
我的模块看起来像......
/* ... */
builder.RegisterControllers(typeof(MvcApplication).Assembly);
/* ... */
builder.RegisterType<RedirectSingleAttribute>();
builder.RegisterType<JsonRedirectAttribute>()
.OnActivating(c => c.Instance.Url = c.Context.Resolve<UrlHelper>());
/* ... */
builder.RegisterFilterProvider();
我在两个OnActionExecuted
方法上都设置了一个断点,JsonRedirectAttribute
始终保持先执行。我尝试撤消订单,并尝试使用AsActionFilterFor
但没有成功。
从action方法中删除属性,并使用autofac注册样式,属性根本不执行。
/* this ended up not working all, i.e. the action filters were never invoked */
builder.RegisterType<RedirectSingleAttribute>()
.AsActionFilterFor<SearchController>(c => c.Query(default(SearchCriteria)), 1);
builder.RegisterType<JsonRedirectAttribute>()
.OnActivating(c => c.Instance.Url = c.Context.Resolve<UrlHelper>())
.AsActionFilterFor<SearchController>(c => c.Query(default(SearchCriteria)), 2);
答案 0 :(得分:1)
您是否尝试过设置“范围”枚举值以及“订单”属性。这个msdn链接可能会引发一些亮点:https://msdn.microsoft.com/en-us/library/gg416513(VS.98).aspx
感谢。