为分层系统设计MVC URL方案

发布于 2024-08-10 14:16:59 字数 635 浏览 6 评论 0原文

想象一下我正在使用 MVC Web 应用程序构建一个多用户 Dungeon 系统。为了描述玩家可以探索的区域,系统可以包含许多地图,其中包括房间和门 - 其中门连接两个房间。 考虑系统的创作部分。创建地图很简单 - 我需要如下 URL:

/Author/Maps              (an index of my maps)
/Author/Maps/Create       (a new Map)
/Author/Maps/Detail/3     (show Map details)
/Author/Maps/Edit/3       (edit map details)

使用路由方案: /Author/{controller}/{action}/{ID}

这是我需要帮助的房间的 URL。创建新房间时,我需要知道要为哪个地图创建它。

/Author/Rooms/CreateIn/[mapID] ?

然后编辑房间的详细信息:

/Author/Rooms/Edit/[roomID]
/Author/Rooms/Detail/[roomID]

这个路由方案有效吗?列出地图所有房间的视图应该是房间控制器上的“索引”操作(传入 MapID),还是地图控制器上的“房间”操作?

谢谢。

So imagine I'm building a Multi User Dungeon system using a MVC web application. To describe the areas the player can explore, the system can contain a number of Maps, which will consist of Rooms and Doors - where doors connect two Rooms.
Consider the authoring part of the system. To create a Map is easy - I need URLs like:

/Author/Maps              (an index of my maps)
/Author/Maps/Create       (a new Map)
/Author/Maps/Detail/3     (show Map details)
/Author/Maps/Edit/3       (edit map details)

Using a Routing scheme: /Author/{controller}/{action}/{ID}

It's the URLs for the Rooms that I need help with. When creating a new Room, I need to know which Map I'm creating it for.

/Author/Rooms/CreateIn/[mapID] ?

And then for editing a room's details:

/Author/Rooms/Edit/[roomID]
/Author/Rooms/Detail/[roomID]

Would this routing scheme work? And should the view that lists all the Rooms for a Map be an "Index" action on the Rooms controller, with a MapID passed in, or a "Rooms" action on a Map controller?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

兔姬 2024-08-17 14:16:59

我不知道这是否是最佳实践,但我会这样做:

添加新路线:/Author/Maps/{mapID}/Rooms/{action}/{roomID}

这是我只希望用于 RoomsController 的路由,该路由中没有 {controller} 参数。只需将控制器设置为路线默认对象中的“房间”即可。

然后,RoomsController 中的所有操作都会知道它们正在使用哪个地图。

RoomsController 的默认索引操作可以是指定地图的所有房间的列表。

创建操作将类似于 Create(int mapID),详细信息操作将类似于 Details(int mapID, int roomID)

编辑:如果 mapID 和 roomID 不匹配,则 URL 无效,您可以忽略 mapID,但我认为更好的过程是验证指定的 mapID 是否正确,如果不正确则显示错误消息。

编辑2:(关于mapID和roomID之间关系的其他想法)
您可以使 roomID 在给定地图中唯一。因此,地图 5 的房间 3 与地图 8 的房间 3 是不同的房间。

I don't know if this is best practice, but I would do it like this:

Add a new route: /Author/Maps/{mapID}/Rooms/{action}/{roomID}

Since this is a route that I would only expect to use for the RoomsController, I wouldn't have a {controller} parameter in that route. Just set the controller to "Rooms" in the route's default object.

Then all the actions in your RoomsController would know which map they're working with.

The default Index action for RoomsController could be a list of all the Rooms for the specified map.

The Create action would look like Create(int mapID) and the Details action would look like Details(int mapID, int roomID)

Edit: regarding invalid URLs with a mismatched mapID and roomID, you could just ignore the mapID, but I think the better process would be to validate that the specified mapID is correct and show an error message if it is not.

Edit 2: (additional thoughts regarding the relationship between mapID and roomID)
You could just make roomID unique within the given map. Therefore, map 5, room 3 would be a different room than map 8, room 3.

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