我正在从 Angular 2 前端发送值,如下所示:
censusManagementSearch(search: any): Observable<any> {
let headers = new Headers({ 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + localStorage.getItem('token') });
let options = new RequestOptions({ headers: headers });
let body = JSON.stringify({ practice: search.practice, location: search.location, name: search.name });
return this.http.post(this._api.apiUrl + 'censusmanagement', body, options)
.map((response: Response) => {
return response;
})
.catch((err: any) => {
return Observable.throw(err.json().error || 'Server error');
});
}
那里的一切工作正常。它将转到 C# 后端,如下所示。如果重要的话,它是一个 ASP.Net 核心 web api
[HttpPost]
public IActionResult PostCensusManagement([FromBody] int practice, int location, string name)
当我在此设置断点时,整数的值为 0,字符串的值为 ""。我试过取出[FromBody]
并用其他选项替换它。每次值为空。
我找到了答案。我会把它留在这里以防其他人遇到这个问题。为了让 web api 看到数据,我必须为它创建一个类。
public class CensusSearch
{
public int location { get; set; }
public int practice { get; set; }
public string name { get; set; }
}
并将控制器更改为:
[HttpPost]
public IActionResult PostCensusManagement([FromBody] CensusSearch search)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句