2017-09-01 8 views
0

[HttpPost]の代わりに[HttpPost("[action]")]を使用する必要がある理由がわかりません。[HttpPost( "[action]")]の代わりに[HttpPost]を使用できないのはなぜですか?

[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(); 
+0

を使用することができますlink

ルートを定義するためのものです。あなたは 'HttpPost(" yourpath \ {id} \ something ")]' –

答えて

3

この[HttpPost("blah blah")]はまた、あなたがパスを指定する必要がAttribute routingRead this article

[HttpPost] 
[Route("PostLeaveRequest")] 
public async Task<IActionResult> PostLeaveRequest([FromBody] LeaveRequest leaveRequest) 
{ 
.................................. 

} 
+0

がうまくいけばそれを置くこともできます。 – simbada

関連する問題