我正在尝试传递 3 个参数(2 种类型和 1 个字符串), 但在服务器端参数为空
这是我的服务
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json '
})
};
let body = {
auditId: auditId,
rolId: this.permisoService.currentUserRolValue.rolId,
valores: JSON.stringify(valores),
}
return this.http.post<any>(this._saveURL, body, httpOptions).pipe(
map(res => { return res; }),
catchError(this.handleError)
);
}
和服务端
[HttpPost, Route("AuditMail/Save"), Produces("application/json")]
public async Task<IActionResult> Save([FromBody] int auditId, int rolId , String valores)
{
return Json(await _repository.Save(auditId, rolId, valores));
}
我尝试将内容类型更改为文本/纯文本,删除 [FromBody] 得到相同的结果,
最佳答案
请参阅类似问题的答案:https://stackoverflow.com/a/49082613/12431728其中说:
You can not get primitive types from your body directly like
[FromBody] string id
if your route content type is application/json because mvc waits model to bind json body to model not primitive types.
我建议在您的 API 项目中创建一个新类来保存这些值:
public class AuditMailModel
{
public int AuditId { get; set; }
public int RolId { get; set; }
public string Valores { get; set; }
}
然后在你的 Controller 中:
public async Task<IActionResult> Save([FromBody] AuditMailModel model)
{
return Json(await _repository.Save(model.AuditId, model.RolId, model.Valores));
}
关于javascript - c# 从 Angular 发送 3 个参数到 c#mvc,我在服务器端得到 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59228519/