vendredi 10 février 2017

Can WebApi parse parameters from an anonymous object posted as JSON?

Vote count: 0

My code doesn't seem to work:

var postData = new { CatName = "Muffins" };

_httpClient.DefaultRequestHeaders.Clear();
_httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

var responseMessage = await _httpClient.PostAsJsonAsync(url, postData);

Then in my controller:

public async Task<CatResponse> SetCatName([FromBody] string CatName)

However CatName is blank. I would have expected MVC to be smart enough to deserialize it from the body, so I am not sure if my code has a bug in it, or if I have to use another technique to do this.

It's possible that it wants to deserialise the anonymous object into a class, but I was hoping to be able to send multiple parameters in the body like with CatName so I can't just send the string on its own.

asked 18 secs ago

Let's block ads! (Why?)



Can WebApi parse parameters from an anonymous object posted as JSON?

Aucun commentaire:

Enregistrer un commentaire