I have the following:
- request url: 'endpoint / 1,2,3? q = foo '
- action to which the request is bound: public object Bar ([ModelBinder] List <T> identifiers, string [FromUri] q)
I want to map the fragment "1,2,3" to the parameter "ids", so I created ModelBinderProvider according to this link , which should call the proper connecting device.
public class MyModelBinderProvider: ModelBinderProvider { public override IModelBinder GetBinder(HttpConfiguration configuration, Type modelType) { IModelBinder modelBinder = null; if (modelType.IsGenericType && (modelType.GetGenericTypeDefinition() == typeof(List<>))) { modelBinder = new ListModelBinder(); } return modelBinder; } }
I registered the provider in Global.asax as follows:
GlobalConfiguration.Configuration.Services.Insert(typeof(ModelBinderProvider), 0, new MyModelBinderProvider());
Reason . I created this provider because I want, no matter what T ("1,2,3" or "one, two, three"), binding to work.
Problem : Let T be 'int'; every time a request is sent, the parameter "modelType" is always "int", and not what I expect - "List <int> ', so the request is not processed properly.
The strange thing : Doing something like this works, but T is specialized, and therefore not what I want:
var simpleProvider = new SimpleModelBinderProvider(typeof(List<int>), new ListModelBinder()); GlobalConfiguration.Configuration.Services.Insert(typeof(ModelBinderProvider), 0, simpleProvider);
I do not see what I am doing wrong, why is the parameter modelType not expected?