Nested resources in ASP.net MVC 4 WebApi

前端 未结 4 611
深忆病人
深忆病人 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:53

    I don't like using the concept of "actions" in the route of an ASP.NET Web API. The action in REST is supposed to be the HTTP Verb. I implemented my solution in a somewhat generic and somewhat elegant way by simply using the concept of a parent controller.

    https://stackoverflow.com/a/15341810/326110

    Below is that answer reproduced in full because I'm not sure what to do when one post answers two SO questions :(


    I wanted to handle this in a more general way, instead of wiring up a ChildController directly with controller = "Child", as Abhijit Kadam did. I have several child controllers and didn't want to have to map a specific route for each one, with controller = "ChildX" and controller = "ChildY" over and over.

    My WebApiConfig looks like this:

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

    My parent controllers are very standard, and match the default route above. A sample child controller looks like this:

    public class CommentController : ApiController
    {
        // GET api/product/5/comment
        public string Get(ParentController parentController, string parentId)
        {
            return "This is the comment controller with parent of "
            + parentId + ", which is a " + parentController.ToString();
        }
        // GET api/product/5/comment/122
        public string Get(ParentController parentController, string parentId,
            string id)
        {
            return "You are looking for comment " + id + " under parent "
                + parentId + ", which is a "
                + parentController.ToString();
        }
    }
    public enum ParentController
    {
        Product
    }
    

    Some drawbacks of my implementation

    • As you can see, I used an enum, so I'm still having to manage parent controllers in two separate places. It could have just as easily been a string parameter, but I wanted to prevent api/crazy-non-existent-parent/5/comment/122 from working.
    • There's probably a way to use reflection or something to do this on the fly without managing it separetly, but this works for me for now.
    • It doesn't support children of children.

    There's probably a better solution that's even more general, but like I said, this works for me.

提交回复
热议问题