因此,我设置了Web Api 2,并从Angular 5进行了轻松的调用。我有一个自定义路由,我想调用该路由,但始终收到400错误。有人可以照亮一点。谢谢。
Web API端:
[Route("api/ViewAllRecords/GetApprovalRecords/{ upn }")]
public IQueryable GetViewAllRecordsForMgrApproval([FromBody]string upn)
{
var set = db.ViewAllRecords.Where(record => record.ApproverUPN ==
upn).AsQueryable();
return db.ViewAllRecords;
}
角度侧:
GetRecordForApproval(upn) {
return this.http.get(environment.apiUrl + '/ViewAllRecords/GetApprovalRecords', { params: {
upn : upn
}});
}
答案 0 :(得分:1)
相关动作的定义存在一些问题。
[FromBody]
无法用于HTTP GET请求,因为它们没有身体
//GET api/ViewAllRecords/GetApprovalRecords/upn_value_here
[HttpGet]
[Route("api/ViewAllRecords/GetApprovalRecords/{upn}")]
public IQueryable GetViewAllRecordsForMgrApproval(string upn) {
var set = db.ViewAllRecords.Where(record => record.ApproverUPN == upn).AsQueryable();
return db.ViewAllRecords;
}
其次,路由模板中有upn
,用于定义URL,但客户端未调用与模板匹配的URL。
更新从客户端调用的URL
GetRecordForApproval(upn) {
var url = environment.apiUrl + '/ViewAllRecords/GetApprovalRecords/' + upn;
return this.http.get(url);
}