简体   繁体   中英

how to receive json values on API call in ASP.NET Core

I have an API with a HttpPost function, but when I call the API from JavaScript, the method variables are always 0. I use [FromBody], is this right?

Function:

[HttpPost("API/AddToCart")]
public async Task AddToCartAsync([FromBody] int id, [FromBody] int count)

The json I pass to the function:

{ "id": id, "count": count }

The function gets called but without the supplied values. Everything works if I use HttpGet and passes the data as query.

Any tips?

Created a model to receive the values.

Model:

public class AddToCartModel
{
    public int Id { get; set; }
    public int Count { get; set; }
}

Action:

public async Task AddToCartAsync([FromBody] AddToCartModel data)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM