Designing an MVC URL Schema for a Hierarchical System

So, imagine that I am creating a multi-user Dungeon system using the MVC web application. To describe the areas that a player can explore, the system may contain several cards, which will consist of a “Room and Door”, where the doors connect the two Rooms. Consider the author’s part of the system. Creating a map is easy - I need URLs, for example:

  / 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 the routing scheme: / Author / {controller} / {action} / {ID}

These are the URLs for the rooms I need help with. When creating a new room, I need to know which map I am creating it to.

  / Author / Rooms / CreateIn / [mapID]?

And then to edit the details of the room:

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

Will this routing scheme work? And should there be a view that lists all the "Rooms for the map", this is the "Index" action in the "Room" controller, with the transmitted MapID or the "Number" action on the map controller?

Thanks.

+4
source share
1 answer

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 expect to use only for RoomController, I would not have the {controller} parameter on that route. Just set the controller to “Rooms” in the default route object.

Then all the actions in your RoomController will know which card they are working with.

The default index action for a RoomController can be a list of all rooms for a specified map.

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

Edit: For invalid URLs with an inconsistent mapID and roomID, you can just ignore mapID, but I think that the best process would be to verify the mapID is correct and display an error message if it isn’t.

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

+5
source

All Articles