2016-08-01 4 views
0

웹 API 2를 사용하고 ASP.Net 4에서 개발 중입니다.이 샘플 코드는 webapi를 배우려고합니다. 두 가지 경로가 있습니다. 첫 번째 경로는 주어진 상점의 서비스 자원입니다. 두 번째 경로는 저장소 리소스 경로입니다.왜 웹 API가 내 리소스를 찾지 못합니까?

public static void RegisterRoutes(RouteCollection routes) 
{ 
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 

    routes.MapRoute(
     name: "Services", 
     url: "store/{id}/services", 
     defaults: new { controller = "Services" } 
    ); 

    routes.MapRoute(
     name: "Store", 
     url: "store/{id}", 
     defaults: new { controller = "Store", id = UrlParameter.Optional} 
    ); 
} 

두 번째 경로 "저장소"는 완벽하게 작동합니다. 첫 번째 경로는 상점에서 사용할 수있는 모든 서비스를 자세하게 설명하는 것입니다. 내가

/API/저장/1/서비스

을하려고 할 때 404 오류가 발생합니다. 누군가 내가 뭘 잘못하고 있는지 가리킬 수 있습니까?

다음은 컨트롤러

namespace APITestter1.Controllers 
{ 
    public class ServicesController : ApiController 
    { 
     public string Get(int id, string prop = "xxx") 
     { 
      return "Hello services World!" + id + " added attribute " + prop; 
     } 

     public string Post(int id, string prop = "xxx") 
     { 
      return "Hello Post World!" + id + " added attribute " + prop; 
     } 

    } 
} 

답변

1

"접두사에"api "와"prop "가있는 이유를 모르겠습니까?

public static void RegisterRoutes(RouteCollection routes) 
    { 
     routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 

     routes.MapRoute(
      name: "Services", 
      url: "api/store/{id}/services/{prop}", 
      defaults: new { controller = "Services", action = "store", prop = UrlParameter.Optional } 
     ); 

     routes.MapRoute(
      name: "Store", 
      url: "store/{id}", 
      defaults: new { controller = "Store", id = UrlParameter.Optional} 
     ); 
    } 


namespace APITestter1.Controllers 
{ 
    public class ServicesController : ApiController 
    { 
     [HttpGet, ActionName="store"] 
     public string Get(int id, string prop = "xxx") 
     { 
      return "Hello services World!" + id + " added attribute " + prop; 
     } 

     [HttpPost, ActionName="store"] 
     public string Post(int id, string prop = "xxx") 
     { 
      return "Hello Post World!" + id + " added attribute " + prop; 
     } 

    } 
} 
:

난 당신의 코드를 업데이트

1

당신은 잘못된 파일에 웹 API 경로를 매핑하는 것입니다. 당신이 웹 api를하고있는 경우에 WebApiConfig.cs 파일을 찾고 당신의 웹 api 노선을 거기에서지도로 나타내십시오.

또한 /api/store/1/services을 탐색하려고하지만이 예에서는 경로를 store/{id}/services으로 매핑합니다. 경로 템플릿에 api이 없습니다.

다음 웹 API를 설정하면

public static class WebApiConfig 
{ 
    public static void Register(HttpConfiguration config) 
    { 
     // Attribute routing. 
     config.MapHttpAttributeRoutes(); 

     // Convention-based routing. 
     config.Routes.MapHttpRoute(
      name: "ServicesApi", 
      routeTemplate: "api/store/{id}/services", 
      defaults: new { controller = "Services" } 
     ); 

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

을 무엇을하려고 일치해야합니다 당신 또한 당신의 두 번째 경로 "스토어"완벽하게 작동하지만 당신은 무엇을 사용 URL 보여 주었다하지 않은 말한다. /api/store/1이라고 가정하면 api/{controller}/{id}을 사용하는 DefaultApi 경로 템플릿에 매핑되기 때문에 작동합니다.

관련 문제