1

내가 이상한 상황이 올바른 경로를 일치하지 않습니다 ... 나는이 API 메소드가 : 나는 api/something/test-something-1를 호출 할왜 내 API는

[Route("api/something")] 
public class MyController : Controller 
{ 
    [HttpGet("test-{id}")] 
    public Task<T> method1() { ... } 

    [HttpGet("test-something-{id}")] 
    public Task<T> method2() { ... } 
} 

을하지만 내 API는

이유 api/something/test-1를 호출?

+1

읽기 [질문]과 [mcve] 제공합니다. 이 코드에는 너무 많은 잘못이 있습니다. 답을 확인할 수있는 기계 뒤에 있지 않은 경우 질문을 게시하지 말고 가능한 한 기다리십시오. – CodeCaster

답변

-2

클래스 선언은 라우팅 속성을 활성화해야 경로 설정 파일에서

[RoutePrifix("api/something")] 
public Class blabla : Controller 
{ 
    [HttpGet("test-{id}")] 
    public Task<T> method1{} 

    [HttpGet("test-something-{id}")] 
    public Task<T> method1{} 
} 

올바르지 않습니다. 템플릿 매개 변수가 id = something-1로 끝날 것

routes.MapMvcAttributeRoutes(); 
+0

클래스 선언이 좋다면 클래스를 작성했을 때 돌진했습니다 : D – Alex

+0

오탈자 ('class','RoutePrefix'가 아니라'RoutePrefix'가 아닌 클래스)가 아닌 질문에 대답하지 않습니다. – CodeCaster

2

경로 "test-{id}" 일치 api/something/test-something-1. 당신이 api/something/test-something-1를 호출 할 때이 같은 경로 충돌 후, 당신은 더 나은 경로를 차별화하는 경로 제약 조건을 사용해야있다 경로 템플릿 test-{id}

method1를 호출하는 이유는

. id이뿐만 아니라 int로 가정하면

[Route("api/something")] 
public class MyController : Controller { 
    [HttpGet("test-{id:int}")]//Matches GET api/something/test-1 
    public Task<IActionResult> method1(int id) { 
     //... 
    } 

    [HttpGet("test-something-{id}")]//Matches GET api/something/test-something-any_id_here 
    public Task<IActionResult> method2(string id) { 
     //... 
    } 
} 

당신은뿐만 아니라 두 번째로 라우팅 제한을 적용 할 수 있습니다.

[HttpGet("test-something-{id:int}")]//Matches GET api/something/test-something-1 
public Task<IActionResult> method2(int id) { 
    //... 
} 

참조 : Routing in ASP.NET Core : Route Constraint Reference