다음과 같이 웹 API 컨트롤러에 다섯 가지 동작이 있습니다.모든 작업에 대한 웹 API 단일 사용자 지정 라우팅
http://localhost:1234/products
- getallproduct
행동
http://localhost:1234/products/1
지도로 - 매핑 할 getproductnyid
조치를
http://localhost:1234/products
-saveproduct
행동 (게시물)
http://localhost:1234/products/1/getcolor
-getproductcolorbyid
행동
http://localhost:1234/products/1/getcost
- 나는 하나의 사용자 정의가 필요
getproductcostbyid
행동 이를위한 URL을 라우팅합니다.
다음 라우팅을 시도했지만 URL에 조치 이름을 추가합니다 (http://localhost:24007/product/GetProductByID/Id
). 원하지 않습니다.
[RoutePrefix("products")]
public class ProductsController : ApiController {
[HttpGet]
[Route("")]
public IHttpActionResult GetAllProduct()
{
//...
}
[HttpGet]
[Route("{productId}")]
public IHttpActionResult GetProductById(int id)
{
//...
}
[HttpPost]
[Route("")]
public IHttpActionResult SaveProduct(Product product)
{
//...
}
[HttpGet]
[Route("{productId}/getcolor")]
public IHttpActionResult GetProductColorById(int id)
{
//...
}
[HttpGet]
[Route("{productId}/getcost")]
public IHttpActionResult GetProductCostById(int id)
{
//...
}
}
그리고 당신의 HttpConfiguration
객체를 등록 해주십시오 : AS를
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "ProductRoute",
routeTemplate: "product/{action}/{productId}",
defaults: new { controller = "Product", productId= RouteParameter.Optional }
);
감사합니다. 요구 사항에 따라 우리는 속성 라우팅을 사용해서는 안됩니다. –
다른 옵션이없는 것 같습니다. –
getcolors, getcost와 같은 URL을 사용해서는 안되는 이유를 설명해 주시겠습니까? –