Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you structure your URL routes?

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:

  1. User/{user id}/Products/List, User/{user id}/Products/Edit/{product id}
  2. User/{user id}/Products, User/{user id}/Products/{product id}
  3. Products?UserID={user id}, Products/Edit/{product id}

I'm sure there are plenty of others that I'm missing. Any advice?

like image 927
Kevin Pang Avatar asked Sep 23 '08 06:09

Kevin Pang


People also ask

How is a URL structure?

A URL consists of five parts: the scheme, subdomain, top-level domain, second-level domain, and subdirectory.

How do I route a URL?

URL routing allows you to configure an application to accept request URLs that do not map to physical files. A request URL is simply the URL a user enters into their browser to find a page on your web site.

Which file the URL routing is specified?

In the ASP.NET Web Forms application, every URL must match with a specific . aspx file.

What is a URL rule?

A URL rule is a class implementing the yii\web\UrlRuleInterface, usually yii\web\UrlRule. Each URL rule consists of a pattern used for matching the path info part of URLs, a route, and a few query parameters. A URL rule can be used to parse a request if its pattern matches the requested URL.


2 Answers

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=140 doesn'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/troethom it 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.

like image 106
Troels Thomsen Avatar answered Sep 22 '22 10:09

Troels Thomsen


You may want to take a look at the question "Friendly url scheme?".

Particularly, Larry.Smithmier's answer provided a list of common URL schemes when using MVC in ASP.NET.

like image 20
coobird Avatar answered Sep 19 '22 10:09

coobird