post 使用 httpClient Asp.net 核心 2.0 和 angular5

post using httpClient Asp.net core 2.0 and angular5

我有一个带有 httpPost 操作的控制器 webapi class,

[HttpPost("[action]")]
public async Task<IActionResult> AddBrokerContact( [FromBody] BrokerContact broker)
 {
     if (broker!=null && !string.IsNullOrEmpty(broker.BrokerFirstName)) {         
          _context.Add(broker);
          await _context.SaveChangesAsync();
          return CreatedAtAction("Post", broker);
     }
     else
     {
       return BadRequest("Need More Data");
     }
 }

我的问题是,当我使用 httpClient 从 angular5 发出请求时,我在控制器中将代理对象设为 null,这是我的服务 class

AddnewBrokerContact(broker: BrokerClass): Observable<BrokerClass> {
  var headers = new HttpHeaders();
  headers.append('Content-Type', 'application/json; charset=utf-8');
  return this.http.post<BrokerClass>(this.baseUrl + 'api/BrokerContact/AddBrokerContact', broker, { headers});
}

在服务方法中使用和接口而不是 Class 解决了我的问题,谢谢

AddnewBrokerContact(broker: BrokerInterface): Observable<BrokerInterface> {
  var headers = new HttpHeaders();
  headers.append('Content-Type', 'application/json; charset=utf-8');
  return this.http.post<BrokerInterface>(this.baseUrl + 'api/BrokerContact/AddBrokerContact', broker, { headers});
}