How do you structure URL routes?

Is there a specific pattern that developers usually follow? I have never thought about this before in my web applications, but the ASP.NET MVC routing mechanism pretty much forces you to at least take it into account.

So far, I liked the structure of the controller / action / index (e.g. Products / Edit / 1), but I am struggling with more complex URLs.

For example, let's say you have a page that lists all the products that a user has in his account. How would you do that? At the top of my head, I can recall the following options for the combo page and 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 am sure that there are many others that I miss. Any tips?

+7
url asp.net-mvc routing
source share
6 answers

I like RESTful, user friendly and hacked URLs.

What does it mean? Let's start with user-friendly URLs. For me, a user-friendly URL is easy to remember and easy to remember /Default.aspx?action=show&userID=140 does not meet any of these requirements. A url like `/ users / troethom 'seems logical.

This leads to the next point. Hacking URL is a URL that a user can change and get as a result. If the URL is hacked and my profile URL is /users/troethom , it would be safe to remove my username to get a list of users ( /users ).

Using RESTful URLs is very similar to the ideas behind my other suggestions. You design URLs for the user, not the machine, so the URL should refer to the content, not the technical content of your site. A URL like "/ users" makes more sense than "/ users / list", and a URL like "/ category / programming / javascript" (representing the subcategory "javascript" in the programming category is better than "/ category / show / 12 '.

Indeed, it’s more difficult to omit identifiers, but in my world it is worth the effort.

Also consult the "Understanding URIs" section for general W3C HTTP implementation issues. It has a list of common errors when developing a URI. Another good resource is Resourceful Vs Hackable Search URLs .

+7
source share

You can take a look at the question " Friendly url scheme? ".

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

+3
source share

In addition, you can use different verbs to reuse the same routes for different actions. For example, a GET request for "Products / Edit / 45" will display the product editor, while POST at the same URL will update the product. You can use the AcceptVerb attribute to accomplish this:

 [AcceptVerb("GET")] public ActionResult Edit(int id) { ViewData["Product"] = _products.Get(id); return View(); } [AcceptVerb("POST")] public ActionResult Edit(int id, string title, string description) { _products.Update(id, title, description); TempData["Message"] = "Changes saved successfully!"; return RedirectToAction("Edit", new { id }); } 
+1
source share

Bill de hΓ“ra wrote a very good essay entitled Web resource comparison criteria for frameworks that are worth reading.

0
source share

To add troethom to comments, RESTful also usually means that, for example, to create a new user, you must PUT view / users / newusername

RESTful mainly uses 5 standard HTTP methods (GET, PUT, POST, DELETE, HEAD) to control / access the content.

Well, this is not easy for a web browser, but you can always use an overloaded POST (a message in / users / username with a view of the user about changing some details, etc.

A good way to do this, I would recommend reading RESTFul Web services to better understand: D (and this is a damn good book!)

0
source share

I saw two main ways to approach this topic ...

One of them is described in the MvcContrib Project Documentation

and the other is described on the blog by Stephen Walter (which I personally prefer).

0
source share

All Articles