[英]400 Bad request Error when http post from angular into .NET Core Web API
I am trying to post some values to my .NET core web api function from Angular. I am trying to post some values to my .NET core web api function from Angular. Its triggering an 400 bad request error.
它触发了 400 错误请求错误。 At the same time Get is working fine.
同时Get工作正常。
Here are my Web APIs这是我的 Web API
[HttpGet]
public List<GenExternalActualDeletionLog> Get()
{
return _svc.DeletionLogs();
}
// POST api/<DeleteExternalActuals>
[HttpPost]
public ActionResult<List<GenExternalActualDeletionLog>> Post(String pCode,string[] expenseKeys)
{
return Ok(_svc.DeleteExternalActual(pCode, expenseKeys, _caller.Identity.Name).OrderByDescending(x => x.DeletedOn));
}
And here is my angular service calling the web apis这是我的 angular 服务调用 web api
DeleteData(configuration: ExternalActualsDeleteData): Observable<any> {
this.baseUrl = this.conf.getSettings("apiUrl");
let hd = new HttpHeaders();
hd.append('Content-Type', 'application/json');
const headers = new HttpHeaders().set("Content-Type", 'application/json');
//alert(this.baseUrl +'/api/closure/ClosureOverride');
console.info(configuration);
return this.http.post(this.baseUrl + '/api/DeleteExternalActuals', {pCode:configuration.pCode,expenseKeys:configuration.expenseKeys});
}
GetAllDeleteLogs(): Observable<ExternalActualsDeleteLog[]> {
this.baseUrl = this.conf.getSettings("apiUrl");
return this.http.get<ExternalActualsDeleteLog[]>(this.baseUrl + "/api/DeleteExternalActuals").pipe(
catchError((err) => {
console.log('error caught in service')
console.error(err);
//Handle the error here
return throwError(err); //Rethrow it back to component
})
);
} }
Here only post shows error.这里只有帖子显示错误。 Get working fine.
好好工作吧。 Is it because of the way I am sending parameter?
是因为我发送参数的方式吗? or anything else?
还是别的什么? The call is not even reaching the server side api.
呼叫甚至没有到达服务器端 api。 Thats why I thought about the parameter
这就是我考虑参数的原因
I think you should wrap your data with Dto and set the FromBodyAttribute .我认为你应该用 Dto 包装你的数据并设置FromBodyAttribute 。
Try this:尝试这个:
public record PostDeleteExternalActualsDto(string pCode, string[] expenseKeys);
[HttpPost]
public ActionResult<List<GenExternalActualDeletionLog>> Post([FromBody] PostDeleteExternalActualsDto data)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.