2014-04-15 4 views
0

ASP.NET MVC처럼 간단하지만 URI의 문자열 매개 변수를 내 컨트롤러의 동작에 매핑하는 방법을 알 수 없습니다.문자열 매개 변수로 동작하는 웹 API 라우팅

GetSocieteByLibelle (캐릭터 이름)

내 경로 구성 : 그래서 조치와 함께 URI를 매핑하고 싶은

//GET: api/Societe 
    public IEnumerable<Societe> GetSociete() 
    { 
     List<Societe> listeSociete = Librairie.Societes.getAllSociete(); 
     return listeSociete.ToList();       
    } 


    //GET: api/Societe/id 
    [ResponseType(typeof(Societe))] 
    public IHttpActionResult GetSociete(int id) 
    { 
     Societe societeRecherchee = Librairie.Societes.getSociete(id); 
     if (societeRecherchee == null) 
     { 
      return NotFound(); 
     } 
     else 
     { 
      return Ok(societeRecherchee); 
     } 
    } 


    public IHttpActionResult GetSocieteByLibelle(string name) 
    { 
     Societe societeRecherchee = new Societe(); 
     if (!string.IsNullOrWhiteSpace(name)) 
     { 
      societeRecherchee = Librairie.Societes.getSocieteByLibelle(name); 
      if (societeRecherchee == null) 
      { 
       return NotFound(); 
      } 
     } 
     return Ok(societeRecherchee); 
    } 

: 그래서 다음과 같이 내 컨트롤러의 세 가지 작업을해야 Wep API 프로젝트의 기본 설정입니다. 어떤 사람이 URI를 해당 동작에 매핑하는 방법을 설명해 주시겠습니까? 미리 감사드립니다!

답변

2

두 경로가 동일한 메소드 호출이 호출되는 것처럼 보입니다. 다음 중 하나에 [경로] 속성을 넣어보십시오 :

[Route("api/Societe/libelle/{name}")] 
public IHttpActionResult GetSocieteByLibelle(string name) 
{ 

} 

참고 기본/API/소시/{ID}이 여전히 첫 번째 액션을 공격해야한다고.