c# - Sending a List or Array to Web Api -
i creating api method add result textfile.. app reads data text file , populate generic list of c# structure call resultlines. when testing app when inspecting request results line added boby parameters , request using restsharp comsume api.. when put breakpoint on api can see 23 lines has been recieve values null expanding model marked bold
here code consume api
public heatresultresponse addheatwithlist(list<resultfileline> rec,string heatnum, string webapiurl) { restclient client = new restclient(); client.baseurl = new uri(webapiurl); var request = new restrequest("controller/insertnewheatlist", method.post); request.addheader("accept", "application/form-urlencoded"); //client.addhandler("application/json", new jsondeserializer()); request.addheader("content-type", "application/json"); request.requestformat = dataformat.json; request.addparameter("heatnumber", heatnum); rec.foreach(r => request.addparameter("resultlines", r) ); irestresponse<heatresultresponse> response2 = client.execute<heatresultresponse>(request); return response2.data; }
my api method looks this
public getheatidresponse insertnewheatlist(**heatresultinsertlistrequest model**) { getheatidresponse resp = new getheatidresponse { issuccess = false, message = "", heatid = 0 }; if (model == null) { resp.message = "request body empty. should contain json data"; } else { try { if (modelstate.isvalid) { heat heat = new heat(); foreach(resultfileline model1 in model.resultlines) { .... here there code populate result db } } }
here model
public class heatresultinsertlistrequest : baserequest { [datamember] public list<resultfileline> resultlines { get; set; } public string heatnumber { get; set; } }
web api default allow 4mb request data.
iis default accept 26 mb.
so iis accept request app accept 4mb data. thats why request object coming null
you can increase modifying maxrequestlength in httpruntime in web.config
<httpruntime maxrequestlength="50000"></httpruntime>
Comments
Post a Comment