SteinTech
SteinTech

Reputation: 4068

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 ?

Upvotes: 2

Views: 457

Answers (1)

SteinTech
SteinTech

Reputation: 4068

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)

Upvotes: 3

Related Questions