c# - Web API get query params -


i've got following method definition in web api controller:

public async task<ihttpactionresult> get([fromuri] paging pageparams) 

and corresponding paging class 'something' like:

public class paging {     public int pageindex { get; set; }     public int pagesize { get; set; }      public columns order { get; set; } } 

received request /api/search?order(description)=asc&pageindex=2&pagesize=25 i'd not have limited control.

what correct columns class definition in order binding order parameter?

you can implement custom model binder , parse request match object:

here example:

public class column {     public string name { get; set; }     public string ascdesc { get; set; } }      public class searchcustomdatabinder : defaultmodelbinder {      public override object bindmodel(controllercontext controllercontext, modelbindingcontext bindingcontext)     {             if (bindingcontext.modeltype != typeof(paging))                 return base.bindmodel(controllercontext, bindingcontext);              httprequestbase request = controllercontext.httpcontext.request;              int pageindex = request.form.get("pageindex");             int pagesize = request.form.get("pagesize");              // assumes have 1 column sort on each   request , first thing appear in url, can idea extend if have more columns (or post example of url several columns , update answer)               var sortcolumn = regex.match(request.form[0], @"\(([^)]*)\)").groups[1].value;               var ascdesc = request.form.get($"order({sortcolumn}"));               var column = new column();              column.name = sortcolumn;              column.ascdesc = ascdesc;               return new paging();              paging.pageindex = pageindex;              paging.pagesize = pagesize;              paging.order = column;     } }  

register custom model binder:

protected void application_start() {     [...]     modelbinders.binders.add(typeof(paging), new searchcustombinder()); } 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -