In refactoring a lot of MVC code, I have run into an issue with my URL design. For example, let’s say we have a Venue
object
public class Venue
{
public long ID { get; set; }
public List<Event> Events { get; set; }
}
and an Event
object
public class Event
{
public long ID { get; set; }
public Venue Venue { get; set; }
}
So my initial set up was to have an action set up in the EventController
like
public ActionResult List (long? ID) // http://example.org/API/Events/List/12
where the ID
specified the Venue, so it would return a list of events for that venue. If an ID
is not specified, it returns a list of all events. This results in a confusing URL, because the ID
parameter is ambiguous and misleading.
I then thought to change it to
public ActionResult List (long? VenueID) // http://example.org/API/Events/List?VenueID=12
which makes a lot more sense and is more clear.
An even cleaner URL would exist if the action was moved to the VenueController
and set up like
public ActionResult Events (long? ID) // http://example.org/API/Venue/12/Events
as the ID
would clearly specify the Venue
‘s ID
. The issue with this URL is that you are primarily dealing with Event
objects in the VenueController
, which seems wrong.
I have been leaning towards the first option (http://example.org/API/Events/List?VenueID=12
) because, even though the other option is cleaner, it seems like I should keep the Event
pages (as I view this List page as more related to the Event
object) in the EventController
. Any recommendations?
Your best answer is going to be: Decide what you want it to look like, then modify the routing to match.
You can customize how the routing engine processes URLs. See here for examples. Our website uses MVC, but you wouldn’t be able to tell it from the URLs.
In your specific case, I’d suggest making a route which looks for the pattern "Venue/{id}/Events"
and routes it to the Event
controller.
3