Is /resource or /resource/ more preferred with RESTish websites?
Currently the routing framework I have does not treat /resource
and /resource/
the same. So which URL form is more preferred?
/products
or
/products/
Or should I strive to support both?
Currently I am treating it all like this:
/products/ (index)
/products/198
/products/edit/192
Is there a preferred form?
Note that if you use products
(no trailing slash), then relative links to a "child" resource must repeat the "parent" resource's path segment. That is, if you use products
, then you must write <a href='products/123'>
, but if you use products/
, then you can write just <a href='123'>
. If you're returning lots of such links, that can result in significant overhead. See http://www.aminus.org/rbre/shoji/shoji-draft-02.txt section 3.3.2 for a more detailed discussion.
Wikipedia says one thing, but look at stackoverflow itself - it uses /tags
for example. I don't think this makes any differences for a user.
There's no one right way to construct restful urls, but in my own work, I always use urls ending with a slash to return resource collections and resources without an ending slash to reference atomic resources.
I would use /products
mainly because of Rails. There endings like .xml
and .json
specify the response format. In that case /products/.xml
wouldn't make much sense.
I use URLs with a trailing slash to indicate indices, or lists of subordinate resources. A URL with no trailing slash generally indicates an individual resource. One of the rationalizations I use for this, is the behavior of the 'ls -l' command on symbolic links to directories. If you do an 'ls -l' on a symbolic link to a directory and include the trailing slash, you get the contents of the directory it points to, but if you so an ls and don't include the slash, you see that it's a symbolic link.
精彩评论