2

이 내 컨트롤러는 모습입니다 같은 :속성 라우팅이 작동하지 않는 이유는 무엇입니까?

AmbiguousActionException :

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

내가 다시 다음과 같은 예외가 얻을 다음과 같이 내가 URL을 /api/Clients/Get을 요청하지만

[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) 
    { 
    } 
} 

: 여러 작업이 일치했습니다. 다음 작업 경로 데이터를 일치하는 모든 제약 조건이 만족했다 : Assessment.Web.Controllers.ClientsController.Details (Assessment.Web)

Assessment.Web.Controllers.ClientsController.Index (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

합니다.

당신은 api/Clients에 매핑과 컨트롤러의 모든 행동 노선에 그 접두사 것 컨트롤러

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

에 경로를 가지고있다.

은 그래서

[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); 
} 

때문에 컨트롤러의 경로 접두어의 api/Clients/api/Client/Get에 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에 리팩토링해야한다가 삭제 조치에 속성 경로를 업데이트해야하고 IActionResult

[HttpDelete("Delete/{id:int}")] //Matches DELETE api/Clients/Delete/5 
public IActionResult Delete(int id) { 
    //...code removed for brevity 
} 
관련 문제