Using asp webapi to post a nested resource

后端 未结 1 1099
不思量自难忘°
不思量自难忘° 2021-02-09 04:06

I am using mvc webapi to create a REST API and struggling to find an example dealing with POSTs to nested resources.

Basically, I want to POST a comment to

1条回答
  •  暖寄归人
    2021-02-09 04:40

    For nested resources I would suggest that you create very specific routes for the controllers/actions that you want to access.

    routes.MapHttpRoute(
        name: "Posts Routes",
        routeTemplate: "api/posts/{postId}/comments/{commentID}",
        defaults: new { controller = "Posts", action="CommentsForPosts" }
    );
    
    public HttpResponseMessage CommentsForPosts(int postId, int commentID) {
        //go to work
    }
    

    There's no convention in the framework for nested resources but routing gives you the flexibility to map your controllers, methods, and URIs however you see fit

    0 讨论(0)
提交回复
热议问题