0

I am trying to figure out a way to send a Json formatted data to a web api controller in a neat(more natural) way.

let me explain. Suppose I have this controller:

[HttpPost]
public class StudentController : ApiController
{

    public void PostSomething([FromBody] string name, [FromBody] Student s) 
    {
        //do something
    }
}

The json data that I WANT to post is something like this (as it is correctly formatted):

{
    "name" : "John",
    "student" : {
        "id" : "1",
        "age" : "22"
    }
}

But what I SHOULD send for the web api to parameter bind the objects should be like this:

{
    "John",
    {
        "id" : "1",
        "age" : "22"
    }
}

The problem is that if I use my desired json format, both name and student objects will be null in the PostSomething method of the controller.

How can I send a json request with a format similar to the first example to my web api controller?

roostaamir
  • 1,928
  • 5
  • 24
  • 51
  • The second example doesn't look like valid json to me, and fails on every validator I have tested it with. – Peter Morris Jan 20 '17 at 09:03
  • That is exactly the case and you are totally right. But the web api controller only accepta the parameters if I send them this way. That is the problem – roostaamir Jan 20 '17 at 11:21
  • Your question isn't entirely clear to me. Is that the JSON you need to receive from a 3rd party, or is that the JSON format you need to create in order to send to a 3rd party? – Peter Morris Jan 20 '17 at 13:04
  • The first example is how i WANT to send the json request, the web api controller won't accept it. The second example is how web api accepts the json(and I dont like it to be this way) – roostaamir Jan 20 '17 at 15:28
  • 1
    Then you need to write your own descendant of the ParameterBindingAttribute and use that instead of [FromBody] to decorate your parameter. Problem is you will also have to parse this string yourself because it doesn't adhere to any standard - that's going to be a real pain. Can't you get the user to send a properly formatted request? – Peter Morris Jan 20 '17 at 16:02
  • It is more of a matter of being neat. The user is actually an android application that is being made by our team too. Since we are using `Gson` for model binding in our client, we wanted to post json data like my first example. But thank you for you time. I solved the problem using rboe's answer below. – roostaamir Jan 20 '17 at 17:08

2 Answers2

1

In order to consume the desired JSON structure you can change the method signature of the PostSomething and introduce a class that represents the sent data. E.g.

public class StudentTransferObject {
    public string Name {get; set;}
    public Student Student {get; set;}
}

With the Controller:

[HttpPost]
public class StudentController : ApiController
{    
    public void PostSomething([FromBody] StudentTransferObject studentInformation) 
    {
        //do something
    }
}
Ralf Bönning
  • 14,515
  • 5
  • 49
  • 67
1
  1. Read text from response body and parse the objects yourself:

    [HttpPost] public async Task<string> PostSomething() { string result = await Request.Content.ReadAsStringAsync(); //parse here how you want return result; }

  2. Dynamic serialization with custom binding or JToken.

Community
  • 1
  • 1
Petre T
  • 472
  • 5
  • 12