简体   繁体   English

如何在 API 调用 ASP.NET Core 时接收 json 值

[英]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?我有一个 API 和一个 HttpPost function,但是当我从 JavaScript 调用 API 时,方法变量始终为 0。我使用 [FromBody],这是对的吗?

Function: Function:

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

The json I pass to the function: json我传给function:

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

The function gets called but without the supplied values. function 被调用但没有提供值。 Everything works if I use HttpGet and passes the data as query.如果我使用 HttpGet 并将数据作为查询传递,一切正常。

Any tips?有小费吗?

Created a model to receive the values.创建了一个 model 来接收这些值。

Model: Model:

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

Action:行动:

public async Task AddToCartAsync([FromBody] AddToCartModel data)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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