c# webapi 未接收到 JS 数组

JS Array is not received by c# webapi

我有一个测试 js 函数,它应该 post 通过 Post 方法 api 将数据传输到网络 api

function test() {
    var puffs = [];
    var puffObject = {
        id: "2735943",
        priority: "1"
    };

    puffs.push(puffObject);
    puffs.push(puffObject);

    var dto =
    {
        po: puffs
    };
    $.ajax({
        type: "POST",
        url: "../api/PuffPriority/",
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify(dto),
        dataType: "json",
        success: function (data, status) {
            if (data) {
                console.log("Welcome");
            } else {
                console.log("No data");
            }
        },
        error: function (error) {
            console.log("Error");
        }
    });
}

在控制器中class我有

public void Post(PuffPriority[] po){
//Do something here with po, but po is always empty
}

其中 PuffPriority 模型是

public class PuffPriority
{
    public string id { get; set; }
    public string priority { get; set; }
}

我不知道怎么了,这些值是由 JS 发送的,但是 api 控制器不获取它:( 请帮忙,我已经浪费了很多时间。

您的代码中有错误。 只需更改

url: "../api/PuffPriority/"

url: "../api/Post/"

或将您的方法名称从 Post 更改为 PuffPriority

已更改

public void Post(PuffPriority[] po){ 
//Do something here with po, but po is always empty 
}

[FromBody]List<PuffPriority> po{
//Do something here with po, but po is always empty
}