连字符、下划线或驼峰式大小写作为 URI 中的单词分隔符? [英] Hyphen, underscore, or camelCase as word delimiter in URIs?
问题描述
我正在为 Intranet 应用设计基于 HTTP 的 API.我意识到从整体上看这是一个很小的问题,但是:我应该使用连字符、下划线或驼峰命名法来分隔 URI 中的单词吗?
I'm designing an HTTP-based API for an intranet app. I realize it's a pretty small concern in the grand scheme of things, but: should I use hyphens, underscores, or camelCase to delimit words in the URIs?
这是我的初步想法:
驼峰式
- 如果服务器不区分大小写,可能会出现问题
- 似乎在查询字符串键(http://api.example.com?**searchQuery**=...),但不在其他 URI 部分
- possible issues if server is case-insensitive
- seems to have fairly widespread use in query string keys (http://api.example.com?**searchQuery**=...), but not in other URI parts
连字符
- 比其他替代品更美观
- 似乎广泛用于URI的路径部分
- 从未在野外见过带连字符的查询字符串键
- 可能对 SEO 更好(这可能是一个神话)
- more aesthetically pleasing than the other alternatives
- seems to be widely used in the path portion of the URI
- never seen hyphenated query string key in the wild
- possibly better for SEO (this may be a myth)
下划线
- 可能更易于编程语言处理
- 一些流行的 API(Facebook、Netflix、
StackExchange等)在 URI 的所有部分都使用下划线.
- potentially easier for programming languages to handle
- several popular APIs (Facebook, Netflix,
StackExchange, etc.) are using underscores in all parts of the URI.
我倾向于使用下划线来表示所有内容.大多数大玩家都在使用它们的事实令人信服(参见 https://stackoverflow.com/a/608458/360570)a>).
I'm leaning towards underscores for everything. The fact that most of the big players are using them is compelling (see https://stackoverflow.com/a/608458/360570).
推荐答案
您应该在可抓取的 Web 应用程序 URL 中使用连字符.为什么?因为连字符分隔单词(以便搜索引擎可以索引单个单词),并且连字符不是 单词字符.下划线是一个单词字符,意味着它应该被视为单词的一部分.
You should use hyphens in a crawlable web application URL. Why? Because the hyphen separates words (so that a search engine can index the individual words), and a hyphen is not a word character. Underscore is a word character, meaning it should be considered part of a word.
在 Chrome 中双击这个:camelCase
在 Chrome 中双击它:under_score
在 Chrome 中双击它: hyphen-ated
Double-click this in Chrome: camelCase
Double-click this in Chrome: under_score
Double-click this in Chrome: hyphen-ated
看看 Chrome(我听说 Google 也制作了一个搜索引擎)如何只认为其中一个是两个词?
See how Chrome (I hear Google makes a search engine too) only thinks one of those is two words?
camelCase
和 underscore
也要求用户使用 shift 键,而 hyphenated
不需要.
camelCase
and underscore
also require the user to use the shift key, whereas hyphenated
does not.
因此,如果您应该在可抓取的 Web 应用程序中使用连字符,为什么要费心在 Intranet 应用程序中做一些不同的事情呢?少要记住的一件事.
So if you should use hyphens in a crawlable web application, why would you bother doing something different in an intranet application? One less thing to remember.
这篇关于连字符、下划线或驼峰式大小写作为 URI 中的单词分隔符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!