.net 通过的Web Api参数为null
因此,我试图通过Angular服务中的put调用将字符串传递给我的Web Api。该服务实际上在Web Api端调用该方法,但该参数始终通过null传递。有人能给我点光吗,谢谢 下面是代码的样子: Web Api:(断点设置在第一个括号上) 角度:.net 通过的Web Api参数为null,.net,angular,.net,Angular,因此,我试图通过Angular服务中的put调用将字符串传递给我的Web Api。该服务实际上在Web Api端调用该方法,但该参数始终通过null传递。有人能给我点光吗,谢谢 下面是代码的样子: Web Api:(断点设置在第一个括号上) 角度: UpdateManagerEmail(userId){ userId = '577f1e6e-146d-49f1-a354-b31349e7cb49'; const headers = new HttpHeaders();
UpdateManagerEmail(userId){
userId = '577f1e6e-146d-49f1-a354-b31349e7cb49';
const headers = new HttpHeaders();
headers.append('Content-Type', 'application/json');
this._http.put('http://localhost:428/api/User', {
userId : userId
}).subscribe(returnVal => {
console.log('returned');
});
}
要让后端在您发送的请求正文中找到userId,它必须是其中唯一的值。您应该将
内容类型更改为:应用程序/x-www-form-urlencoded;charset=UTF-8
,不带密钥发送数据
文件备注:
在发送简单类型之前,请考虑以复杂类型代替值。这为您提供了在服务器端进行模型验证的好处,并使您在需要时更容易扩展模型
资料来源:
另外,它比我解释得更好。你在开发者工具的“网络”选项卡中的“请求”中看到发送的参数了吗?我可以在请求付费中看到它。你尝试过删除[FromBody]吗?如果没有FromBody,就不会调用它控制器。你尝试过用引号将字符串括起来吗<代码>此。_http.put('http://localhost:428/api/User“,”“'+userId+”)
或设置变量var data=userId
并将数据传递到put方法<代码>此。_http.put('http://localhost:428/api/User,数据)
UpdateManagerEmail(userId){
userId = '577f1e6e-146d-49f1-a354-b31349e7cb49';
const headers = new HttpHeaders();
headers.append('Content-Type', 'application/json');
this._http.put('http://localhost:428/api/User', {
userId : userId
}).subscribe(returnVal => {
console.log('returned');
});
}