c# - Binding a comma separated list to a List<int> in asp.net web api -
the default binder in web api expecst
http://url.com/webapi/report/?pageids=3243&pageids=2365
to bind
public ihttpactionresult report(list<int> pageids){ // exciting webapi code}
i wish bind http://url.com/webapi/report/?pageids=3243,2365
as running out of space in url get
.
i have created class public class commaseparatedmodelbinder :
system.web.http.modelbinding.imodelbinder { public bool bindmodel(httpactioncontext actioncontext, modelbindingcontext bindingcontext) { //binding in here } }
and registered in webapiconfig.cs
var provider = new simplemodelbinderprovider( typeof(list<int>), new commaseparatedmodelbinder()); config.services.insert(typeof(modelbinderprovider), 0, provider);
i have altered method signature use model binder so
public ihttpactionresult report( [modelbinder] list<int> pageids){ // exciting webapi code}
however break point in binder not being hit (and list not bound).
what else need configure?
make sure follow all of steps in article: parameter binding in asp.net web api
it appears missing last step:
with model-binding provider, still need add [modelbinder] attribute parameter, tell web api should use model binder , not media-type formatter. don’t need specify type of model binder in attribute:
public httpresponsemessage get([modelbinder] geopoint location) { ... }
also, i've never tried binding list<int>
. may not able model bind because build-in type. if so, box custom class type , make sure add [modelbinder]
attribute class.
or....
a better solution: kiss
public ihttpactionresult report(string pageids) { var ids = pageids.split(','); // exciting web api code and/or more robust checking of split }
Comments
Post a Comment