C# $http post将值作为null传递给Asp.Net Web API

C# $http post将值作为null传递给Asp.Net Web API,c#,angularjs,asp.net-web-api,C#,Angularjs,Asp.net Web Api,我有一个ASP.NETWebAPI和一个post方法。我从angular js调用post方法。它没有将数据传递给API POST方法,我的requestData对象的所有属性都为null。我不确定我在这里犯了什么错误。有人能帮我吗 API代码 public void Post(RequestData data) { ..... } public class RequestData { PropertyDetail propertyDetails; ICollection<

我有一个ASP.NETWebAPI和一个post方法。我从angular js调用post方法。它没有将数据传递给API POST方法,我的requestData对象的所有属性都为null。我不确定我在这里犯了什么错误。有人能帮我吗

API代码

public void Post(RequestData data)
{
.....
}

public class RequestData
{
    PropertyDetail propertyDetails;
    ICollection<Model1> model1s;
    ICollection<Model2> model2s;
    ICollection<Model3> model3;
    ICollection<Model4> model4;
}

可能是服务器端无法正确映射参数。发布某些参数时,数据类型匹配非常重要。您可以按如下方式更改客户端代码:

...
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify(requestData),
        dataType:'json',
...

在按照@Jaky71所说的做了之后,您可以通过调用带有null的伪方法或任何您需要模拟的方法来了解.net是如何期望这些对象的


.net有一个严格的解析器

您可以使用angular.toJson:

$http({
        method: 'POST',
        url: window.apiUrl,
        headers: { 'Content-Type': 'application/json' },
        data: angular.toJson(requesData),
    }).then(function (res) {
        console.log('succes !', res.data);
        window.alert("Successfully created");
    }).catch(function (err) {
        debugger;
        console.log('error...', err);
    });  

还要确保属性匹配。

确保从js文件接收到的模型与.cs文件中模型上的每个属性的名称同步,这些名称到目前为止是不同的。实际上,我在这里用虚拟名称更改了实际属性名称model0、model1,实际上,我注意到下面这行代码导致发布整个html页面。标题:{'Content Type':'application/x-www-form-urlencoded'},如果删除此标题,将引发另一个错误,在严格模式下访问函数的'arguments'属性是不允许的。实际上,我注意到下面的行导致发布整个html页面。标题:{'Content Type':'application/x-www-form-urlencoded'},如果删除此标题,则会引发另一个错误,在严格模式下不允许访问函数的'arguments'属性您不应该删除它,只需将其更改为application/json即可
$http({
        method: 'POST',
        url: window.apiUrl,
        headers: { 'Content-Type': 'application/json' },
        data: angular.toJson(requesData),
    }).then(function (res) {
        console.log('succes !', res.data);
        window.alert("Successfully created");
    }).catch(function (err) {
        debugger;
        console.log('error...', err);
    });