-
Notifications
You must be signed in to change notification settings - Fork 85
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Multiple filter of same type on Action method #25
Comments
Could please describe the problem a bit more with some pseudo-code. Thanks Am 27.08.2012 um 15:34 schrieb bsohi [email protected]:
|
//attribute and filter
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
public class InjectSelectListAttribute : Attribute {
public List<ListValueCategories> List { get; private set; }
//Named parameter
public bool optionalParameter { get; set; }
public InjectSelectListAttribute(params ListValueCategories[] lists) {
List = lists.ToList();
}
}
public class InjectSelectListFilter : IActionFilter {
private readonly IList<ListValueCategories> _lists;
provate readonly bool _optionalParameter;
public InjectSelectListFilter(List<ListValueCategories> lists, bool optionalParameter) {
_optionalParameter = optionalParameter;
_lists = lists;
}
public void OnActionExecuting(ActionExecutingContext filterContext) {
//do logic based on "optionalParameter"
}
public void OnActionExecuted(ActionExecutedContext filterContext) {
}
}
//Controller
public class LenderController : Controller{
//
[InjectSelectList(List1)]
[InjectSelectList(List2, optionalParameter=true)]
public ActionResult Test(int id) {
//do logic here
}
} If I do this way then i get "sequence contains no element" exception. //Ninject binding
kernel.BindFilter<InjectSelectListFilter>(FilterScope.Action, 0)
.WhenActionMethodHas<InjectSelectListAttribute>()
.WithConstructorArgumentFromActionAttribute<InjectSelectListAttribute>("lists", attribute => attribute.List)
.WithConstructorArgumentFromActionAttribute<InjectSelectListAttribute>("optionalParameter", attribute => attribute.optionalParameter); //so instead I am doing this kernel.BindFilter<InjectSelectListFilter>(FilterScope.Action, 0)
.When((controllerContext, action) => action.GetCustomAttributes(typeof(InjectSelectListAttribute), true).Length > 0)
.WithConstructorArgument("lists", (context, controller, action) => {
object attr = action.GetCustomAttributes(typeof(InjectSelectListAttribute), true);
return ((InjectSelectListAttribute)attr[0]).List;
})
.WithConstructorArgument("optionalParameter", (context, controller, action) => {
object attr = action.GetCustomAttributes(typeof(InjectSelectListAttribute), true);
return ((InjectSelectListAttribute)attr[0]).optionalParameter;
}); for second attribute on same action kernel.BindFilter<InjectSelectListFilter>(FilterScope.Action, 0)
.When((controllerContext, action) => action.GetCustomAttributes(typeof(InjectSelectListAttribute), true).Length > 1)
.WithConstructorArgument("lists", (context, controller, action) => {
object attr = action.GetCustomAttributes(typeof(InjectSelectListAttribute), true);
return ((InjectSelectListAttribute)attr[1]).List;
})
.WithConstructorArgument("optionalParameter", (context, controller, action) => {
object attr = action.GetCustomAttributes(typeof(InjectSelectListAttribute), true);
return ((InjectSelectListAttribute)attr[1]).optionalParameter;
}); Thanx |
Edited comments from bsohi, still needs review |
Just wondering, any planning to fix this issues?? thx |
Added in 3.0.2 |
I know it is supported but we have to repeat binding for filter every time we repeat filter on action method. Is there any reason that it is not supported to bind all at once.
For example:- if we have filter "A" and we want to apply it to one of action method "B" 2 times then we need to bind it twice via ninject.
thx
Balwinder
The text was updated successfully, but these errors were encountered: