简体   繁体   English

使用httpClient Asp.net core 2.0和angular5发布

[英]post using httpClient Asp.net core 2.0 and angular5

i have a controller webapi class with an httpPost action, 我有一个带有httpPost动作的控制器webapi类,

[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");
     }
 }

My problem is that when i make a request from angular5 using httpClient i get the broker object as null in the controller, this is my service class 我的问题是,当我使用httpClient从angular5发出请求时,我在控制器中将代理对象获取为null,这是我的服务类

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});
}

在此处输入图片说明

在此处输入图片说明

using and interface instead of a Class in the service method solve my problem, Thanks 在服务方法中使用and接口而不是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});
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM