带 Restful 路由的 ASP.NET:从资源内访问(子)控制器

发布于 2024-08-14 23:04:55 字数 523 浏览 4 评论 0原文

我不太确定如何表达这一点,但我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

余生共白头 2024-08-21 23:04:55

我喜欢这个概念,我想转到 /Project/ProjectId/Tasks 并查看该项目的任务的筛选视图。然而,就我个人而言,我不介意“特定于项目”的视图(例如详细信息/显示/更新/等)是否返回到/Task/TaskId。

然而,我确实看到它对“依赖”类型有何好处,例如当没有项目就无法存在任务时,可以通过 /Project/ProjectId/Tasks/TaskId 访问它们。

为了实现我的版本,我在项目控制器中有一个操作,如下所示:

public ActionResult Tasks(string id)
{
    Project projectToDisplay = Repository.GetProject(id);
    ViewModel["Tasks"] = Repository.GetTasksForProject(projectToDispaly);
    return View(projectToDisplay);
}

显然,这可以通过强类型 ViewModel 来改进。

作为参考,我的存储库(特定于实体框架)具有以下代码:

public Project GetProject(int id)
{
    return _entities.Projects.FirstOrDefault(m => m.Id == id);
}

public IList<Task> GetTasksForProject(Project project)
{
    return _entities.Tasks.Where(m => m.Project.Id == project.Id).ToList();
}

或者,使用实体框架,您可以在存储库中执行以下操作:

public Project GetProject(int id)
{
    return _entities.Projects.Include("Tasks").FirstOrDefault(m => m.Id == id);
}

然后控制器只需要:

public ActionResult Tasks(string id)
{
    return View(Repository.GetProject(id));
}

这有点更像 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:

public ActionResult Tasks(string id)
{
    Project projectToDisplay = Repository.GetProject(id);
    ViewModel["Tasks"] = Repository.GetTasksForProject(projectToDispaly);
    return View(projectToDisplay);
}

Obviously, this could be improved with a strongly typed ViewModel.

For reference, my repository (Entity Framework specific) has the following code:

public Project GetProject(int id)
{
    return _entities.Projects.FirstOrDefault(m => m.Id == id);
}

public IList<Task> GetTasksForProject(Project project)
{
    return _entities.Tasks.Where(m => m.Project.Id == project.Id).ToList();
}

Alternatively, with Entity Framework, you could just do the following in the repository:

public Project GetProject(int id)
{
    return _entities.Projects.Include("Tasks").FirstOrDefault(m => m.Id == id);
}

And then the controller would just need:

public ActionResult Tasks(string id)
{
    return View(Repository.GetProject(id));
}

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.)

橘和柠 2024-08-21 23:04:55

我找到了答案,那就是 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文