简体   繁体   中英

post using httpClient Asp.net core 2.0 and angular5

i have a controller webapi class with an httpPost action,

[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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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