我有一个使用此Put请求方法的Angular服务:
put(peopleFieldID: number, peopleFieldName: string): Observable<number> {
let params = new HttpParams();
params = params.append("peopleFieldID", peopleFieldID.toString());
params = params.append("peopleFieldName", peopleFieldName);
return this.http
.put<number>(this.url, { params: params })
.pipe(catchError(this._errorHandling.handleError.bind(this)));}
以上命令在我的.net核心API上点击此端点:
[Authorize(Roles = "Client")]
[Route("")]
[HttpPut]
public IActionResult C_Update(int peopleFieldID, string peopleFieldName )
{
//Make call to the proper repo method.
return Json(_peopleFieldRepo.C_Update(peopleFieldID, peopleFieldName));
}
两个参数peopleFieldID和peopleFieldName始终为0且为null。我已经确定Angular前端正确地发送了params,但是后端无法识别它们。我还有很多其他端点可以正常工作。
您需要使用HttpParams
设置查询参数,如果没有,则需要将null
传递给有效负载:
const params = new HttpParams()
.set('peopleFieldID', peopleFieldID.toString())
.set('peopleFieldName', peopleFieldName);
// if you have a payload to pass you can do that in place of null below
return this.http
.put<number>(this.url, null, { params: params })
.pipe(catchError(this._errorHandling.handleError.bind(this)));}