I've just finished following this tutorial: http://ift.tt/1ACZDLU
And I'm trying to accomplish the trivial task of sending an abstract/raw request body without the stupid/useless middle layer in ASP.NET trying to interpret the body contents as a pre-defined class I've already created.
Here's my super simple code ASP.NET seems to %$#& up:
[HttpPost]
[Route("test")]
public string test([FromBody] string json)
{
return json;
}
Which would lead you to think that POSTing to this URI with the following body: "trouble shooting fun" OR "{"info":"troubleshooting fun"}"
would simply result in this response:
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Content-Length: 4
{"info":"troubleshooting fun"}
This is the kind of response you'd expect from all the other web frameworks like Django or Ruby-o-Rails because it just makes sense, but in ASP.NET Web API there is an additional layer working behind the scenes that forces you to create the request and response objects manually by declaring them as local classes like so:
public class hardCodedRequestJson
{
public string keyValuePair { get; set; }
}
So... ummm this is kind of cool, if you want some useless backwards compatibility with XML but this approach comes at the cost of being stuck with extremely specific parameters for each API call, for my situation I'd have to make hundreds of different combinations of classes and functions that account for the dynamic number of possibilities the client must have when making an API call to the server.
How could I feed any kind of JSON object I wanted into an ASP.NET Web API controller and get that EXACT json I requested back as a response??
Aucun commentaire:
Enregistrer un commentaire