Angular 使用FromBody字符串调用web api HttpPost-body始终为空

Angular 使用FromBody字符串调用web api HttpPost-body始终为空,angular,asp.net-web-api2,Angular,Asp.net Web Api2,我真的认为这很简单,但我不能得到这项工作 我有一个c#web api,其方法如下: [HttpPost] public IHttpActionResult UpdateTaskComment(int id,[FromBody]string comment) { //do something } 从客户端,使用angular httpClient,以下是我尝试调用web api方法的方式: let reqHeaders = new HttpHeaders().set('Content-

我真的认为这很简单,但我不能得到这项工作

我有一个c#web api,其方法如下:

[HttpPost]
public IHttpActionResult UpdateTaskComment(int id,[FromBody]string comment)
{
     //do something
}
从客户端,使用angular httpClient,以下是我尝试调用web api方法的方式:

let reqHeaders = new HttpHeaders().set('Content-Type','application/json');
this.http.post(`${BASE_URL}/UpdateTaskComment?id=${id}`,comment,{headers:reqHeaders})
         .subscribe(...)
         //catch error
当调用“到达”服务器时,id是正常的,但注释总是空的

注意事项:

  • 我尝试将FromBody用于简单字符串的原因是它可能是一个很长的字符串
  • 我知道我可以用一个模型来包装字符串,它会工作的,但是我想知道我在这里是否遗漏了一些东西来这样做

我做错了什么?

好吧,最后这是一个非常简单的答案:

只需在string对象上使用JSON.stringify

let reqHeaders = new HttpHeaders().set('Content-Type','application/json');
this.http.post(`${BASE_URL}/UpdateTaskComment?id=${id}`,JSON.stringify(comment),{headers:reqHeaders})
         .subscribe(...)
         //catch error

所以,你知道怎么做,但你不想这么做?