【发布时间】:2019-01-12 11:11:36
【问题描述】:
我正在使用 Angular 5 开发一个应用程序。
http.get 服务在这里工作正常,但在http.post 中出现问题。
下面是我的代码:
GetEmployee() {
//Data needs to be grouped in an object array as payload
var payload = { "StaffCode": this.employeeCode };
this.showLoader = true;
this.http.post<StaffInfo>(this.baseURL + 'api/StaffDetail/GetEmployee', JSON.stringify(payload)).subscribe(result => {
this.Staff = result;
this.showLoader = false;
}, error => console.error(error));
}
.net 核心中的 API:
[HttpPost("[action]")]
public Employee GetEmployee(string StaffCode)
{
return util.GetEmployee(StaffCode);
}
我在点击按钮时调用它
<button type="button" (click)="GetEmployee()" class="btn btn-sm btn-warning">Get Detail</button>
但在我的 API 中它为空。
我是否以错误的方式调用 post API?
还有一件事,如果我在参数签名之前添加[FromBody],它甚至不会命中 API。
【问题讨论】:
-
通常,如果该端点的想法是获取员工,则最好使用 HTTP GET 请求,并通过 URL 传递
StaffCode
标签: c# angular .net-core asp.net-core-webapi