0

product라는 asp.net mvc 컨트롤러가 있습니다.웹 API 경로 wint 동작에는 여러 get 메소드에 대한 이름이 필요합니다.

public class ProductController : ApiController 
{ 
    [HttpGet] 
    public IHttpActionResult Get() 
    { 
     return Ok("product"); 
    } 
} 

내 경로는 이와 같습니다.

config.Routes.MapHttpRoute("DefaultRoute", 
    "api/{controller}/{id}", 
     new { id = RouteParameter.Optional }); 

나는 제품이 URL과 같은 방법을 받기에 액세스 할 수 있습니다 localhost:2541/api/product

그리고 좀 estra가 methots을받을 필요가있다.

public class ProductController : ApiController 
{ 

    [HttpGet] 
    public IHttpActionResult Get() 
    { 
     return Ok("product"); 
    } 

    [HttpGet] 
    public IHttpActionResult Hello() 
    { 
     return Ok("Hello from product"); 
    } 
} 

그리고 새로운 경로를 설정했습니다.

config.Routes.MapHttpRoute("ActionsRoute", 
    "api/{controller}/{action}/{id}", 
    new { id = RouteParameter.Optional }); 

하지만 난에 액세스 할 수 없습니다 localhost:2541/api/product

오류 : {action} 경로 매개 변수를 포함하여 지금 다른 요청에 액션의 이름을 포함 할 필요가 있기 때문에

Multiple actions were found that match the request: Get

답변

0

즉 선택된 행동을 알지 못할 것입니다. like

localhost:2541/api/product/Get 

루트가 여전히 필요한 경우 경로를 매핑 할 때 기본값을 포함하십시오. 그와

config.Routes.MapHttpRoute("ActionsRoute", 
    "api/{controller}/{action}/{id}", 
    new { action = "Get", id = RouteParameter.Optional }); 

아래의 호출을 다음과 같이 매핑합니다, 수행

GET localhost:2541/api/product  --> ProductController.Get 
GET localhost:2541/api/product/Get --> ProductController.Get 
GET localhost:2541/api/product/Hello --> ProductController.Hello