1

for C# WebApi, I used this function to deal with the POST request from my front end, code as below Method1:

[HttpPost]
public object GetPostItem(string Name, string email, int Code)
{
  //perform something here
}       

but it seems like it didn't work, unless I try for Method2

[HttpPost]
public object GetPostItem(info Info)
{
  //This is how I get them from *Info*
  info.Name
  info.email
  info.Code
  //perform something here
}     

Is there any way for letting function POST to received object separately or the only way to deal with POST is Method2 ?

Lawraoke
  • 540
  • 5
  • 25
  • How does the form look like with which you post? – GSerg Jun 16 '20 at 07:29
  • @GSerg It is something like this `{Name= myName, email= a@a.com, Code=0}` FYI my frontend is using **React** – Lawraoke Jun 16 '20 at 07:32
  • I think your question is already answered here: https://stackoverflow.com/questions/14407458/webapi-multiple-put-post-parameters – bigb055 Jun 16 '20 at 07:35

1 Answers1

1

At most one parameter is allowed to read from the message body. And body is default source in post. You can use parameters by FromUri Attribute.

Leszek Mazur
  • 2,443
  • 1
  • 14
  • 28
  • i see: `When a parameter has [FromBody], Web API uses the Content-Type header to select a formatter. In this example, the content type is "application/json" and the request body is a raw JSON string (not a JSON object). At most one parameter is allowed to read from the message body. So this will not work` I think this answer my question – Lawraoke Jun 16 '20 at 07:55
  • So may I conclude that the above **Method1** from question is actually cannot worked? – Lawraoke Jun 16 '20 at 07:58
  • 1
    If you want to use body, and default, build in WebService request handling, yes. – Leszek Mazur Jun 16 '20 at 08:04
  • hmm let's see, so general conclusion, only params (like **HttpGet**) will be using **Method1** while **HttpPost** which pass though body will be using **Method2** – Lawraoke Jun 16 '20 at 09:05
  • 1
    You can use Method2 with [FromUri], but params must be in ulr, not in body. Get not have body, so default source is FromUri, but in post you must add attribute to read from uri. – Leszek Mazur Jun 16 '20 at 09:09
  • yup that make clear for me, appreciated your answer and time, Leszek ;D – Lawraoke Jun 16 '20 at 09:11