Nested resources in ASP.net MVC 4 WebApi

前端 未结 4 608
深忆病人
深忆病人 2020-12-02 07:09

Is there a better way in the new ASP.net MVC 4 WebApi to handle nested resources than setting up a special route for each one? (similar to here: ASP.Net MVC support for Nest

4条回答
  •  悲&欢浪女
    2020-12-02 07:41

    Since Web API 2 you can use Route Attributes to define custom routing per Method, allowing for hierarchical routing

    public class CustomersController : ApiController
    {
        [Route("api/customers/{id:guid}/products")]
        public IEnumerable GetCustomerProducts(Guid id) {
           return new Product[0];
        }
    }
    

    You also need to initialize Attribute Mapping in WebApiConfig.Register(),

      config.MapHttpAttributeRoutes();
    

提交回复
热议问题