.net core asp.net webapi 2 post参数值始终为null(字符串)或0(int)
已经通过几十个链接,都说相同的解决方案。 为api调用中的参数创建一个对象。并使用json传递完全相同的对象 因此,API调用如下(即命中): 输入对象的定义如下:.net core asp.net webapi 2 post参数值始终为null(字符串)或0(int),.net-core,fetch,asp.net-core-webapi,.net Core,Fetch,Asp.net Core Webapi,已经通过几十个链接,都说相同的解决方案。 为api调用中的参数创建一个对象。并使用json传递完全相同的对象 因此,API调用如下(即命中): 输入对象的定义如下: public class SearchParams { public string searchWord { get; set; } public int anid { get; set; } } 因此,呼吁: let item = { searchWord: searchKeywor
public class SearchParams
{
public string searchWord { get; set; }
public int anid { get; set; }
}
因此,呼吁:
let item = {
searchWord: searchKeyword.value.trim(),
anid: 1
};
const uri = "../../WebApi/SearchItems";
fetch(uri, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
body: JSON.stringify(item),
//credentials: 'include',
mode: 'cors',
})
看看网络流量,通过的报头是:
:authority: localhost:44386
:method: POST
:path: /WebApi/SearchItems
:scheme: https
accept: application/json
accept-encoding: gzip, deflate, br
accept-language: en-GB,en-US;q=0.9,en;q=0.8
content-length: 32
content-type: application/json
cookie: .AspNetCore.Session=CfDJ8LGmdU3gpbZIvSkveH0x8cMT8VibUQlc07yQ8SJOo6DJkNqykRsAz2V6NVuQ5zQhzBNiHjZ2iRJc%2Fno44sQdQJhsVPnktzx8EWu%2Bptg9ONjmErDP3TZ1csme%2FAJ3H5hSgvooxH0snE00och2ov4ZldFCosHYGH6X70ESjL8PbcJg; .AspNetCore.Antiforgery.Q2hy0CiNRlg=CfDJ8LGmdU3gpbZIvSkveH0x8cOOBFRZOyah2508xPXIUjTbV_weFLdM06pME-M-kc2l48FOmSym_5JS9GUHJeciQEKJI9SHBu1D-5wLcVF4de3rYsjKRsI67qGrCado7eBFDBAbeYFOLWEMbXXCQr_0vlA
origin: https://localhost:44386
referer: https://localhost:44386/lb_users/Details/11
sec-fetch-dest: empty
sec-fetch-mode: cors
sec-fetch-site: same-origin
user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36
因此,有效载荷为:
{"searchWord":"qwerty","anid":1}
我的VS调试器和邮递员都返回null/0值
对.NETCore来说相对较新。
那么,我在这里遗漏了什么基本的东西,因为我看到的几十个问题似乎都与我在解决方案中添加的问题相同
提前谢谢
编辑:
这很有效
const uri = "../../WebApi/SearchItems";
fetch(uri, {
method: 'POST',
headers: {
"Content-type": "application/x-www-form-urlencoded; charset=UTF-8"
},
body: "searchWord=" + searchKeyword.value.trim() + "&anid=1",
//credentials: 'include',
mode: 'cors',
})
因此,JSON似乎是个问题。
JSON对象是否已从传递给api调用的过程中删除?我测试了asp.net webapi2和asp.net core,两者都可以工作。 以下是asp.net webapi2的演示: ApiController:
public class ValuesController : ApiController
{
[System.Web.Http.HttpPost]
public string GetVal(SearchParams searchParams)
{
return "success";
}
}
public class SearchParams
{
public string searchWord { get; set; }
public int anid { get; set; }
}
阿贾克斯:
薪资水平:
{searchWord: "hghgfgf", anid: 1}
结果:
您还可以尝试创建一个新项目,并对其进行测试
<input id="searchWord"/>
<button onclick="sendData()">ssss</button>
<script>
function sendData() {
let item = {
searchWord: $("#searchWord").val().trim(),
anid: 1
};
const uri = "../api/Values";
fetch(uri, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
body: JSON.stringify(item),
//credentials: 'include',
mode: 'cors',
})
}
</script>
:authority: localhost:44383
:method: POST
:path: /api/Values
:scheme: https
accept: application/json
accept-encoding: gzip, deflate, br
accept-language: en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7
content-length: 33
content-type: application/json
cookie: .AspNetCore.Culture=c%3Den-US%7Cuic%3Den-US; .AspNetCore.Antiforgery.I5tvYLHG_pU=CfDJ8AkZmG9N6OhEnYVb3Xy31rQgFnXqHPkWTaV4nUodZGM9SfyhvD5jztl-kzo768oHkGPUYV-bOoSNBP5OuTHO_yd08w-IxrsO39HceHJgOIqC5ePYLIxXd0w9cBzYeWEu5amihQhOqiLySw376bp9YdM
origin: https://localhost:44383
referer: https://localhost:44383/Home/Index1
sec-fetch-dest: empty
sec-fetch-mode: cors
sec-fetch-site: same-origin
user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36
{searchWord: "hghgfgf", anid: 1}