2

這是我的控制器是什麼樣子:爲什麼我的屬性路由不起作用?

[Route("api/[controller]")] 
[Produces("application/json")] 
public class ClientsController : Controller 
{ 
    private readonly IDataService _clients; 

    public ClientsController(IDataService dataService) 
    { 
     _clients = dataService; 
    } 

    [HttpPost] 
    public int Post([Bind("GivenName,FamilyName,GenderId,DateOfBirth,Id")] Client model) 
    { 
     // NB Implement. 
     return 0; 
    } 

    [HttpGet("api/Client/Get")] 
    [Produces(typeof(IEnumerable<Client>))] 
    public async Task<IActionResult> Get() 
    { 
     var clients = await _clients.ReadAsync(); 
     return Ok(clients); 
    } 

    [HttpGet("api/Client/Get/{id:int}")] 
    [Produces(typeof(Client))] 
    public async Task<IActionResult> Get(int id) 
    { 
     var client = await _clients.ReadAsync(id); 
     if (client == null) 
     { 
      return NotFound(); 
     } 

     return Ok(client); 
    } 

    [HttpGet("api/Client/Put")] 
    public void Put(int id, [FromBody]string value) 
    { 
    } 

    [HttpGet("api/Client/Delete/{id:int}")] 
    public void Delete(int id) 
    { 
    } 
} 

然而,當我請求URL /api/Clients/Get,如下所示:

json = await Client.GetStringAsync("api/Clients/Get"); 

我得到以下異常:

AmbiguousActionException:多個操作匹配。下面 行動匹配的路由數據,並已全部約束滿足:

Assessment.Web.Controllers.ClientsController.Index(Assessment.Web) Assessment.Web.Controllers.ClientsController.Details(Assessment.Web) Assessment.Web .Controllers.ClientsController.Create(Assessment.Web)

ClientHttpClient。請注意,儘管使用相同的名稱id,但沒有GET操作與路線數據匹配。

這裏有什麼問題?

+0

MVC-5? - 看起來像asp.net-core-mvc –

+0

@StephenMuecke它是核心,我的錯誤。我經常混淆了MVC 5和6. – ProfK

+0

這是「ClientsController」的整個界面嗎?異常消息中的3個操作方法不在代碼示例中。如果全部都發布了,我們需要查看其餘的路由配置,以確定哪條路由匹配'api/Clients/Get'的請求。 – NightOwl888

回答

0

我認爲這是在路由一個錯字,它應該在年底,而不是客戶端客戶小號與(一個或多個)。

[HttpGet("api/Clients/Get")] 

代替

[HttpGet("api/Client/Get")] 

或改變調用端點:您正在使用的屬性錯誤

json = await Client.GetStringAsync("api/Client/Get"); 
2

你有控制器

[Route("api/[controller]")] 

這將映射到api/Clients和前綴在控制器的任何行動路線上的路線。

這樣就意味着

[HttpGet("api/Client/Get")] // Matches GET api/Clients/api/Client/Get 
[Produces(typeof(IEnumerable<Client>))] 
public async Task<IActionResult> Get() 
{ 
    var clients = await _clients.ReadAsync(); 
    return Ok(clients); 
} 

匹配,因爲控制器上的路由前綴的GET來api/Clients/api/Client/Get

引用Routing to Controller Actions

您需要更新的動作屬性的路由相應

[Route("api/[controller]")] 
[Produces("application/json")] 
public class ClientsController : Controller { 
    private readonly IDataService _clients; 

    public ClientsController(IDataService dataService) 
    { 
     _clients = dataService; 
    } 

    [HttpPost] //Matches POST api/Clients 
    public int Post([Bind("GivenName,FamilyName,GenderId,DateOfBirth,Id")] Client model) { 
     // NB Implement. 
     return 0; 
    } 

    [HttpGet("Get")] //Matches GET api/Clients/Get 
    [Produces(typeof(IEnumerable<Client>))] 
    public async Task<IActionResult> Get() { 
     //...code removed for brevity 
    } 

    [HttpGet("Get/{id:int}")] //Matches GET api/Clients/Get/5 
    [Produces(typeof(Client))] 
    public async Task<IActionResult> Get(int id) { 
     //...code removed for brevity 
    } 

    [HttpGet("Put")] //Matches PUT api/Clients/Put 
    public void Put(int id, [FromBody]string value) { 
     //...code removed for brevity 
    } 

    [HttpGet("Delete/{id:int}")] //Matches GET api/Clients/Delete/5 
    public void Delete(int id) { 
    } 
} 

刪除動作實際上應該重構爲一個HTTP DELETE,應該返回IActionResult

[HttpDelete("Delete/{id:int}")] //Matches DELETE api/Clients/Delete/5 
public IActionResult Delete(int id) { 
    //...code removed for brevity 
} 
相關問題