How do you structure your URL routes?

2019-04-07 03:15发布

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?

6条回答
孤傲高冷的网名
2楼-- · 2019-04-07 03:54

I've seen two main accepted ways to approach this topic...

One is described in the MvcContrib project documentation

and the other one is described in a blog post by Stephen Walther (which I personally prefer).

查看更多
姐就是有狂的资本
3楼-- · 2019-04-07 03:55

Also, you may consider using different verbs to reuse the same routes for different actions. For example, a GET request to "Products/Edit/45" would display the product editor, whereas a POST to the same url would 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 });
}
查看更多
走好不送
4楼-- · 2019-04-07 03:58

To add to troethom's comments, RESTful generally also means that, for example, to create a new user you would PUT a representation to /users/newusername

RESTful basically uses the 5 standard HTTP Methods (GET, PUT, POST, DELETE, HEAD) for controlling/accessing content.

Ok, this isn't easy for a web browser, but you can always use overloaded POST (post to /users/username with a representation of a user to change some of the details, etc.

Its a good way of doing things, I'd reccommend reading RESTFul Web services to get a better understanding :D (and it's a darn good book!)

查看更多
唯我独甜
5楼-- · 2019-04-07 04:14

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.

查看更多
The star\"
6楼-- · 2019-04-07 04:18

Bill de hÓra wrote a very good essay entitled Web resource mapping criteria for frameworks that is well worth a read.

查看更多
我欲成王,谁敢阻挡
7楼-- · 2019-04-07 04:19

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.

查看更多
登录 后发表回答