你如何构建你的URL路线? [英] How do you structure your URL routes?
问题描述
有没有一个特定的模式,开发商普遍遵循?我从来没有真正赋予它很多在我的web应用程序之前想过,但你至少把它考虑到ASP.NET MVC路由引擎pretty太多的力量。
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.
到目前为止,我喜欢控制器/动作/索引结构(如产品/编辑/ 1),但我挣扎更复杂的URL。
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:
- 用户/ {用户ID} /产品/列表,用户/ {用户ID} /产品/编辑/ {产品ID}
- 用户/ {用户ID} /产品,用户/ {用户ID} /产品/ {产品编号}
- 产品?用户名= {用户ID},产品/编辑/ {产品ID}
我敢肯定有很多人说我失踪。有什么建议?
I'm sure there are plenty of others that I'm missing. Any advice?
推荐答案
我喜欢RESTful的,用户友好和破解的网址。
I like RESTful, user friendly and hackable URLs.
这是什么意思?让我们开始的用户友好的URL 的。对我来说,一个用户友好的URL是什么容易输入易记 /Default.aspx?action=show&userID=140
不符合上述任何要求。像`/用户的URL / troethom'似乎是合乎逻辑,但。
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.
这引出了下一个点。 A 破解的网址的是,用户可以修改,仍然可以得到一个结果psented $ P $的URL。如果URL是容易被破解和我的个人资料的网址是 /用户/ troethom
这将是安全地删除我的用户名来获得用户( /用户
)。
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
).
使用的 REST风格的网址的类似于背着我其他的建议想法pretty。你正在设计的URL为用户而不是一台机器,因此URL必须涉及到的内容和你的网站而不是技术的后端。作为一个URL'/ users'使超过意义上的/用户/ list'和psenting子类别/分类/编程/ javascript'(重$ P $'类别中的'编程JavaScript中的URL为优于 /分类/显示/ 12'。
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´.
这的确是更加难以忽略的ID,但在我的世界里是值得的。
It is indeed more difficult to omit IDs, but in my world it is worth the effort.
还可以参考上W3C's认识的URI部分常见的HTTP执行问题。它在设计时的URI常见陷阱的列表。另一个很好的资源是足智多谋VS破解的搜寻网址。
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.
这篇关于你如何构建你的URL路线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!