ContentType application/json in ASP.NET WebAPI -
i'm building first webapi using asp.net mvc 4 webapi.
the requests must sent using application/json contenttype utf-8 character set.
my post method looks this:
public httpresponsemessage post([frombody]string value) { return new httpresponsemessage(httpstatuscode.ok); } whenever i'm sending post request parameter 'value' null. request body contains json: { "name":"test" }.
what prefer have parameter of post method either string containing json or of type jobject (from json.net library). how accomplish this? , possible?
the easiest way grab raw string directly request.content:
public async task<httpresponsemessage> post() { string value = await request.content.readasstringasync(); return new httpresponsemessage(httpstatuscode.ok); } there way make asp.net web api treat request body string content, in order content must in =value format, in case this:
={ "name":"test" } you can achieve following jquery code (for example):
$.post('api/values', '=' + json.stringify({ name: 'test' })); in case can use signature question.
in end there option of creating own mediatypeformatter replace default jsonmediatypeformatter , make deserialize content jobject. can read more creating , registering mediatypeformatter here , here.
Comments
Post a Comment