How do you structure your URL routes?

自作多情 提交于 2019-12-04 03:56:42

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.

coobird

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.

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 });
}

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

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!)

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).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!