2

모델 컬렉션 (/api/models)에 여전히 모호하지 않은 액세스를 허용하면서 쿼리 매개 변수 기반 경로 (/api/models?id=1)와 경로 기반의 하나 (/api/models/1)를 동시에 지원해야합니다.FromQuery와 FromRoute 매개 변수를 동시에 바인딩하는 방법은 무엇입니까?

내 컨트롤러 보이는이 같은 (뭔가) :

[Route("/api/{controller}")] 
public class ModelsController : Controller 
{ 
    [HttpGet] 
    public Models[] GetModels([FromQuery]QueryOptions queryOptions) 
    { 
     //... 
    }  

    [HttpGet("{id:int}")] 
    public Model Get([FromRoute] int id) 
    { 
     //... 
    } 

    [HttpGet("?{id:int}")] 
    public Model Get2Try1([FromQuery] int id) 
    { 
     //Fails with ": The literal section '?' is invalid. 
     //Literal sections cannot contain the '?' character." 
     //Which makes sense after some reading... 
    } 

    [HttpGet] 
    public Model Get2Try2([FromQuery] int id) 
    { 
     //Fails with "AmbiguousActionException: Multiple actions matched. 
     //The following actions matched route data and had all constraints satisfied: 
     //GetModels and Get2Try2" 
     //Which I think I understand as well...the absence of optional params 
     //means ambiguous routing... 
    } 

    [HttpGet] //What here? 
    public Model Get2Try3([FromQuery] int id) //and/or here? 
    { 

    } 
} 

몇 가지 방법이 있어야한다처럼 내가 (선언적 라우팅) 느낌이 작업을 수행. 이 라인을 따라 아무 일도하지 않았습니까?

또한 현재 코드베이스는 RTM/1.0으로 곧 업그레이드 될 ASP.NET 코어 (RC1)입니다. 양 측면에 대한 세부 사항은 유사 할 수도 있지만 양쪽 모두에 관심이 있습니다.

답변

3

나는 다음과 같은 작품을 발견했습니다 :

[HttpGet, Route("{id?}")] 

... 키가 주로 인 '?'. 메서드 서명에서 [FromX]가 필요 없으며 쿼리 문자열과 경로 매개 변수 전달에 대한 트릭을 수행합니다.

불행하게도 자신감 UI는 그것을 좋아하고 어떤 명시 적 매개 변수는 상자 ( https://github.com/domaindrivendev/Ahoy/issues/47 또는 https://github.com/domaindrivendev/Ahoy/issues/182)에서 작동 할 것으로 예상,하지만 그건 또 다른 이야기 :

+0

이 나를 위해 작동하지 않습니다입니다하지 않습니다. [FromRoute]로 작동하며 [FromQuery]로 작동하지 않습니다. – neeohw