我从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,&#34;&#34;对于字符串。我尝试过[FromBody]
并将其替换为其他选项。每次值都为空。
答案 0 :(得分:1)
我找到了答案。我会留在这里以防其他人遇到这个问题。为了让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)