使用ASP的WebAPI张贴嵌套资源 [英] Using asp webapi to post a nested resource
问题描述
我使用MVC的WebAPI创建一个REST API,并努力找到的POST处理嵌套资源的一个例子。
I am using mvc webapi to create a REST API and struggling to find an example dealing with POSTs to nested resources.
基本上,我想 POST
使用URL喜欢博客文章评论:
Basically, I want to POST
a comment to a blog post using a URL like:
〜/职位/ 2 /评论
我也想能够发送DELETE和使
以下
I would also like to be able to send DELETE and PUTs
to the following
〜/职位/ 2 /评论/ 5
我要报名的路线是什么样子,什么应该在我的 PostsController
是什么样子?我的方法签名
What should my route registration look like, and what should my method signature on my PostsController
look like?
谢谢!
推荐答案
有关嵌套的资源,我建议你创建控制器/你要访问的行动非常具体的路线。
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
}
有在嵌套资源框架没有任何公约,但路由让您灵活映射你的控制器,方法和URI但是您认为合适的。
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
这篇关于使用ASP的WebAPI张贴嵌套资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!