带 Restful 路由的 ASP.NET:从资源内访问(子)控制器
我不太确定如何表达这一点,但我在 RoR 应用程序中经常看到的一种模式是这样的:
/Post/postid/Comment/commentid
或
/Project/projectid/Tasks/taskid
本质上在模型中,因为项目具有您可以从项目资源内访问任务控制器的任务。
现在我开始将 MVCContrib 中的 SimplyRestfulRouting 与 ASP.NET MVC 一起使用,因此我具有以下格式:
Route =>动作
/{控制器} => Index()
/{控制器}/{id} => Show()
我不会详细介绍所有细节,任何人都可以 google SimplyRestfulRouting 来查看。我想知道是否有一致的方法来允许:
/{Controller}/{id}/{Controller}/{id} 类型语法。
显然,如果这是一种优于配置风格的约定,而不仅仅是设置大量路由,那将是理想的选择。
I'm not really sure how to express this, but one pattern I often see in RoR apps is something like this:
/Post/postid/Comment/commentid
or
/Project/projectid/Tasks/taskid
Essentially in the model since a Project has Tasks you can access the TaskController from within a project resource.
Now I have started using the SimplyRestfulRouting from MVCContrib with ASP.NET MVC, so I have the following format:
Route => Action
/{Controller} => Index()
/{Controller}/{id} => Show()
I won't get into all the details, anyone can google SimplyRestfulRouting to see. I am wondering if there is a consistent way to then allow:
/{Controller}/{id}/{Controller}/{id} type syntax.
Obviously it would be ideal if this was a convention over configuration style rather than just setting up lots of routes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我喜欢这个概念,我想转到 /Project/ProjectId/Tasks 并查看该项目的任务的筛选视图。然而,就我个人而言,我不介意“特定于项目”的视图(例如详细信息/显示/更新/等)是否返回到/Task/TaskId。
然而,我确实看到它对“依赖”类型有何好处,例如当没有项目就无法存在任务时,可以通过 /Project/ProjectId/Tasks/TaskId 访问它们。
为了实现我的版本,我在项目控制器中有一个操作,如下所示:
显然,这可以通过强类型 ViewModel 来改进。
作为参考,我的存储库(特定于实体框架)具有以下代码:
或者,使用实体框架,您可以在存储库中执行以下操作:
然后控制器只需要:
这有点更像 ViewModel 风格。
(注意:此代码已适应您的项目/任务上下文,因此它不是我的实际代码。如果此代码不起作用或有错误,我很抱歉,我已经有效地重新编写了它,因此可能存在拼写错误/ ETC。)
I like this concept, I want to go to /Project/ProjectId/Tasks and see a filtered view of tasks for that Project. Personally, I don't mind, however, if the "item specific" views (e.g. details / show / update / etc) are back at /Task/TaskId.
I do, however, see how it could be beneficial for "dependent" types, such as when a task can't exist without a Project, for them to be accessed via /Project/ProjectId/Tasks/TaskId.
To implement my version, I have an Action in the Project controller as follows:
Obviously, this could be improved with a strongly typed ViewModel.
For reference, my repository (Entity Framework specific) has the following code:
Alternatively, with Entity Framework, you could just do the following in the repository:
And then the controller would just need:
which is a little bit more ViewModel-esque.
(NB: This code has been adapted to your project / task context, so it is not my actual code. If this code doesn't work or has errors, I apologise, I've effectively re-written it so there may be typos / etc.)
我找到了答案,那就是 Steve Hodgekiss 的《宁静路线》。它比 MVCContrib 附带的灵活得多,并且可以实现我正在谈论的那种事情以及更多很多。
http://stevehodgkiss.com/2009/ 10/11/restful-routes-for-asp-net-mvc.html
I have found the answer and it is Steve Hodgekiss' Restful Routes. It is much more flexible than the one that comes with MVCContrib and allows for the sort of thing I am talking about and much, much more.
http://stevehodgkiss.com/2009/10/11/restful-routes-for-asp-net-mvc.html