Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebApi GET and multiple POST parameter without model always null

I am trying send both parameter get and post to method on service without model but it's always null. How can i solve this problem? Also what is logical reason of this problem? Codes as follow which i encountered problem;

[HttpGet]
public Result GetUser(string userId)
{
    using (DataContext dc = new DataContext())
    {
        return dc.GetUser(userId);
    }
}

[HttpPost]
public Result CreateUser([FromBody]string firstname, [FromBody]string lastname)
{
    using (DataContext dc = new DataContext())
    {
        return dc.CreateUser(firstname, lastname);
    }
}
like image 596
Goran Zooferic Avatar asked Apr 21 '26 00:04

Goran Zooferic


1 Answers

What you are trying to do in the Post request is not valid as per the HTTP protocol. Each web request can only have 1 body, and while it is possible to pass a single param in a unstructured form it is not possible with two (source Asp.net website here ). Hence you have 2 options:

1). Add the 2 params to the url - Change the [FromBody] to [FromUri]

2). Encapsulate both params into a class/model, and accept this as input to your post method (from body or URL/URi will both work). The sender of the data will be able to specify how they wish to send this, via xml or json, by default with asp.net Web api

like image 130
BMac Avatar answered Apr 22 '26 14:04

BMac



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!