How to get Json Post Values with asp.net webapi
asp.net, asp.net-web-api, post, request
Solution
First way:
public void Post([FromBody]dynamic value)
{
var x = value.var1.Value; // JToken
}
Note that `value.Property` actually returns a `JToken` instance so to get it's value you need to call `value.Property.Value`.
Second way:
public async Task Post()
{
dynamic obj = await Request.Content.ReadAsAsync<JObject>();
var y = obj.var1;
}
Both of the above work using Fiddler. If the first option isn't working for you, try setting the content type to `application/json` to ensure that the `JsonMediaTypeFormatter` is used to deserialize the content.
Problem
i'm making a request do a asp.net webapi Post Method, and i'm not beeing able to get a request variable. Request ``` jQuery.ajax({ url: sURL, type: 'POST', data: {var1:"mytext"}, async: false, dataType: 'json', contentType: 'application/x-www-form-urlencoded; charset=UTF-8' }) .done(function (data) { ... }); ``` WEB API Fnx ``` [AcceptVerbs("POST")] [ActionName("myActionName")] public void DoSomeStuff([FromBody]dynamic value) { //first way var x = value.var1; //Second way var y = Request("var1"); } ``` i Cannot obtain the var1 content in both ways... (unless i create a class for that) how should i do that?