0

동일한 이름 컨트롤러가 포함 된 두 개의 다른 클래스 라이브러리가 있습니다.경로 별 웹 API 컨트롤러 선택

namespace OldApiService{ 
    public class GreetingController: ApiController{ 
     public string Get(){ return "hello from old api"; } 
    } 
} 

namespace NewApiService{ 
    public class GreetingController: ApiController{ 
     public string Get(){ return "hello from new api"; } 
    } 
} 

그리고 루트 및 기타 도우미 클래스가 포함 된 주요 We Api 응용 프로그램이 있습니다. 이 응용 프로그램은 NewApiServiceOldApiService 어셈블리를 참조합니다.

namespace MyApi { 
    public class Startup 
    {    
     public void Configuration(IAppBuilder appBuilder) 
     { 
      HttpConfiguration config = new HttpConfiguration(); 
      config.Routes.MapHttpRoute( 
       name: "DefaultApi", 
       routeTemplate: "api/{api}/{controller}/{id}", 
       defaults: new { id = RouteParameter.Optional } 
      ); 

      appBuilder.UseWebApi(config); 
     } 
    } 
} 

url 매개 변수를 지정한 컨트롤러를 선택하고 싶습니다.

http://localhost:4035/api/old/greetingNewApiService 내가 URL 경로 설정을 변경할 수 있지만 일을하지 않도록 노력

컨트롤러를 사용합니다 OldApiService 컨트롤러

http://localhost:4035/api/new/greeting을 사용합니다. 중복 된 컨트롤러 오류가 발생했습니다.

컨트롤러 선택 메커니즘을 무시할 수있는 방법이 있습니까? 단순히 경로 값 (이전 또는 신규)을 가져 와서 지정된 네임 스페이스에서 컨트롤러를 선택합니다.

'인사말'컨트롤러와 일치하는 여러 유형이 발견되었습니다. 이 요청을 처리하는 경로 ('api/{version}/{controller}/{id}')이 동일한 이름이지만 다른 네임 스페이스가 지원되지 않는 으로 정의 된 여러 컨트롤러를 찾은 경우 발생할 수 있습니다. '인사'에 대한 요청은 컨트롤러를 일치 다음을 발견했다 : OldApiService.GreetingController NewApiService.GreetingController

내가이 asp.net 웹 API에 대한 중요한 문제라고 생각합니다.

+2

당신이 두 라이브러리의 컨트롤은 항상있을 것 같이 라우팅 속성을 사용하여 고려하는 경우 컨벤션 기반 라우팅을 통한 갈등 경로를 매핑 할 때 컨트롤 이름에 ks. – Nkosi

+0

RoutePrefix 속성을 사용해 보셨습니까? – peinearydevelopment

+0

예 Route 속성을 시도했지만 작동하지 않았습니다. – barteloma

답변

1

나는 RoutePrefix/Route 속성을 사용할 것이다.

namespace OldApiService{ 
    [RoutePrefix("api/old/greeting")] 
    public class GreetingController: ApiController{ 
     [Route("")] 
     public string Get(){ return "hello from old api"; } 
    } 
} 

namespace NewApiService{ 
    [RoutePrefix("api/new/greeting")] 
    public class GreetingController: ApiController{ 
     [Route("")] 
     public string Get(){ return "hello from new api"; } 
    } 
} 
+0

두 라이브러리의 구성을 노출하는 방법도 보여줍니다. – Nkosi

+0

'인사'라는 컨트롤러와 일치하는 여러 유형이 있습니다. 이 요청을 처리하는 경로 ('api/{version}/{controller}/{id}')가 동일한 이름이지만 다른 네임 스페이스로 정의 된 여러 컨트롤러를 찾았지만 지원되지 않는 경우에 발생할 수 있습니다. 'greeting'에 대한 요청에서 다음 일치하는 컨트롤러를 발견했습니다. OldApiService.GreetingController NewApiService.GreetingController – barteloma

2

올드 라이브러리

namespace OldApiService{ 

    public static class WebApiConfig { 
     public static void Register(HttpConfiguration config) { 
      config.config.MapHttpAttributeRoutes();   
     } 
    } 

    [RoutePrefix("api/old/greeting")] 
    public class GreetingController: ApiController{ 
     [Route("")] 
     public string Get(){ return "hello from old api"; } 
    } 
} 

다른 라이브러리

namespace NewApiService{ 

    public static class WebApiConfig { 
     public static void Register(HttpConfiguration config) { 
      config.config.MapHttpAttributeRoutes();   
     } 
    } 

    [RoutePrefix("api/new/greeting")] 
    public class GreetingController: ApiController{ 
     [Route("")] 
     public string Get(){ return "hello from new api"; } 
    } 
} 

하기 시작하기

namespace MyApi { 

    public class Startup { 
     public void Configuration(IAppBuilder appBuilder) { 

      var config = new HttpConfiguration(); 

      //Map attribute routes 

      OldApiService.WebApiConfig.Register(config); 
      NewApiService.WebApiConfig.Register(config); 

      //convention-based routes 
      config.Routes.MapHttpRoute( 
       name: "DefaultApi", 
       routeTemplate: "api/{api}/{controller}/{id}", 
       defaults: new { id = RouteParameter.Optional } 
      ); 

      appBuilder.UseWebApi(config); 
     } 
    } 

}