c# - Web.Api accept parameter form value -
i using web.api 2.2, client sending data in style:
data={"kind": "conversation", "tags": [], "items": [{"body": "hi there", "timestamp": "1445958749.284379", "kind": "messagetooperator", "nickname": "united kingdom (rickmansworth) #5043", "visitor_nickname": "united kingdom (rickmansworth) #5043"}, {"body": "my name amel , testing olark", "timestamp": "1445958753.320339", "kind": "messagetooperator", "nickname": "united kingdom (rickmansworth) #5043", "visitor_nickname": "united kingdom (rickmansworth) #5043"}, {"body": "hi amel jessica", "timestamp": "1445958763.486881", "kind": "messagetovisitor", "nickname": "jessica wood ", "operatorid": "744399"}, {"body": "ok im back", "timestamp": "1445959002.452643", "kind": "messagetooperator", "nickname": "united kingdom (rickmansworth) #5043", "visitor_nickname": "united kingdom (rickmansworth) #5043"}, {"body": "hello there", "timestamp": "1445959059.642775", "kind": "messagetovisitor", "nickname": "jessica wood ", "operatorid": "744399"}, {"body": "i ma here", "timestamp": "1445959066.829973", "kind": "messagetooperator", "nickname": "united kingdom (rickmansworth) #5043", "visitor_nickname": "united kingdom (rickmansworth) #5043"}, {"body": "test", "timestamp": "1445959885.173931", "kind": "messagetooperator", "nickname": "united kingdom (rickmansworth) #5043", "visitor_nickname": "united kingdom (rickmansworth) #5043"}, {"body": "hi there", "timestamp": "1445959894.323173", "kind": "messagetovisitor", "nickname": "jessica wood ", "operatorid": "744399"}, {"body": "how doing", "timestamp": "1445959900.186131", "kind": "messagetovisitor", "nickname": "jessica wood ", "operatorid": "744399"}, {"body": "testing olark", "timestamp": "1445960829.592606", "kind": "messagetooperator", "nickname": "united kingdom (rickmansworth) #5043", "visitor_nickname": "united kingdom (rickmansworth) #5043"}, {"body": "hello there", "timestamp": "1445960834.471775", "kind": "messagetovisitor", "nickname": "jessica wood ", "operatorid": "744399"}], "operators": {"744399": {"username": "winlotto.com", "emailaddress": "support@winnlotto.com", "kind": "operator", "nickname": "jessica wood ", "id": "744399"}}, "visitor": {"city": "rickmansworth", "kind": "visitor", "conversationbeginpage": "http://www.winnlotto.com/", "countrycode": "gb", "ip": "195.110.84.183", "chat_feedback": {}, "operatingsystem": "windows", "emailaddress": "", "country": "united kingdom", "organization": "colt technology services group limited", "fullname": "united kingdom (rickmansworth) #5043", "id": "ttofv5oa0muga16s7281c5p1goasjja4", "browser": "firefox 41.0"}, "islead": "true", "id": "rwpaff48iti4y6du7281c2r1gp0fhvj3"}
see data= in start. sending json data parameter in value, funny, don't know how accept in web.api controller. tried [frombody] string data, doesn't seem work.
this action accepts it:
[route("index")] [httppost] public ihttpactionresult index([frombody] string data) { var body = string.empty; using (var reader = new streamreader(request.content.readasstreamasync().result)) { reader.basestream.seek(0, seekorigin.begin); body = reader.readtoend(); } return ok(); }
edit: did crated model string data field , retrieves json in string:
public class servicemodel { public string data { get; set; } }
is correct way retrieve form params?
what want create model represents object expecting. caller send object formatted json, web api can automatically deserialize c# object. in object can validation on model, data, , return result can automatically deserialized json (if caller wants).
i recommend studying / reading on core web api concepts basic. not want start implementing , supporting solution not understand. there plenty of great resources out there, if prefer hands on search tutorials, if prefer finer details start book or in depth explanation.
here code started. classes based on json provided.
public class item { public string body {get;set;} public string timestamp {get;set;} // rest of properties } public class someobject { public string kind {get;set;} public string[] tags {get;set;} public item[] items {get;set;} // rest of properties } // method in web api controller public ihttpactionresult index([frombody] someobject myobject) { // validation , action code here , return result } // update web api configuration registration convert camel case json pascal cased c# , again public static class webapiconfig { public static void register(httpconfiguration config) { // makes webapi json results lower case var json = globalconfiguration.configuration.formatters.jsonformatter; json.serializersettings.contractresolver = new camelcasepropertynamescontractresolver(); // rest of code
Comments
Post a Comment