3

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?

1 Answer 1

3

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
  • 1
    You might even customize the router to redirect http://example.org/API/Venue/{id}/Events to http://example.org/API/Events/List?VenueID={id} and offer both URIs. May 6, 2013 at 17:15
  • I hadn't thought about dealing with it using routing. Thanks.
    – wholol
    May 6, 2013 at 17:35
  • @theyshookhands - Yeah, changing the routing isn't an intuitive part of working with MVC, since all the tutorials and examples assume the standard Controller/Action/ID format. But it's powerful. Glad it helped!
    – Bobson
    May 6, 2013 at 18:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.