我不明白爲什麼我需要使用[HttpPost("[action]")]
而不是[HttpPost]
?爲什麼我不能使用[HttpPost]而不是[HttpPost(「[action]」)]?
如果使用[HttpPost("[action]")]
,則來自角客戶端的請求會觸發控制器操作。
// POST: api/LeaveRequests
[HttpPost("[action]")]
public async Task<IActionResult> PostLeaveRequest([FromBody] LeaveRequest leaveRequest)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
_context.LeaveRequests.Add(leaveRequest);
await _context.SaveChangesAsync();
return CreatedAtAction("GetLeaveRequest", new { id = leaveRequest.EmployeeNo }, leaveRequest);
}
但 如果我只是把[HttPost]
然後從anglular客戶端的請求沒有命中控制器動作,而是它命中的方法與路徑[HttpGet("{id}")]
我的MVC路線
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
routes.MapSpaFallbackRoute(
name: "spa-fallback",
defaults: new { controller = "Home", action = "Index" });
});
我的角http.post
return this.http.post(this._apiUrl, body, options)
.map(res => res.json()) // ...and calling .json() on the response to return data
.catch((error: any) => Observable.throw(error.json().error || 'Server error'))
.subscribe();
你需要指定的路徑。你也可以把'[HttpPost(「yourpath \ {id} \ something」)]' –