Is there a specific pattern that developers generally follow? I never really gave it much thought before in my web applications, but the ASP.NET MVC routing engine pretty much forces you to at least take it into consideration.
So far I’ve liked the controller/action/index structure (e.g. Products/Edit/1), but I’m struggling with more complex urls.
For instance, let’s say you have a page that lists all the products a user has in their account. How would you do it? Off the top of my head I can think of the following possibilities for a listing page and an edit page:
- User/{user id}/Products/List, User/{user id}/Products/Edit/{product id}
- User/{user id}/Products, User/{user id}/Products/{product id}
- Products?UserID={user id}, Products/Edit/{product id}
I’m sure there are plenty of others that I’m missing. Any advice?
I like RESTful, user friendly and hackable URLs.
What does this mean? Lets start with user friendly URLs. To me a user friendly URL is something easy to type and easy to remember
/Default.aspx?action=show&userID=140doesn’t meet any of these requirements. A URL like `/users/troethom´ seems logical though.This leads to the next point. A hackable URL is an URL that the user can modify and still get presented with a result. If the URL is hackable and the URL for my profile is
/users/troethomit would be safe to remove my user name to get a list of users (/users).Using RESTful URLs is pretty similar to the ideas behind my other suggestions. You are designing URLs for a user and not for a machine and therefore the URL has to relate to the content and not the the technical back-end of your site. An URL as ´/users´ makes more sense than ´/users/list´ and an URL as ´/category/programming/javascript´ (representing the subcategory ‘javascript’ in the category ‘programming’ is better than ´/category/show/12´.
It is indeed more difficult to omit IDs, but in my world it is worth the effort.
Also consult the Understanding URIs section on W3C´s Common HTTP Implementation Problems. It has a list of common pitfalls when designing URIs. Another good resource is Resourceful Vs Hackable Search URLs.