西纳特拉网址 '/'解释
我是一个 ruby 新手,已经尝试 Sinatra 有一段时间了,我无法弄清楚为什么 url 中的“/”会产生如此大的差异。 我的意思是不是:
get 'some_url' do
end
和
get 'some_url/' do
end
应该指向同一条路线?为什么 Sinatra 将其视为不同的路线?我花了整整一个小时试图弄清楚这一点。
I am a ruby newbie and have been trying Sinatra for quite some time now, one thing that Iam not able to figure out is why does a '/' in the url make such a big difference.
I mean isnt:
get 'some_url' do
end
and
get 'some_url/' do
end
Supposed to point to the same route? why is that Sinatra considers it as different routes? I spent a good one hour trying to figure that out.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据 RFC 2616 和 RFC 2396(定义资源标识的 RFC),这些 URL 不定义相同的资源。因此西纳特拉对他们区别对待。这是特别是。如果您想象路由返回带有相关链接的页面,则这一点很重要。 此链接
如果您来自
/foo
,则 将指向/bar
;如果您来自,则此链接将指向
./foo/bar
>/foo/您可以使用以下语法来定义与两者匹配的路由:
或上面注释中提到的正则表达式版本。
According to RFC 2616 and RFC 2396 (RFCs defining resource identity) those URLs do not define the same resource. Therefore Sinatra treats them differently. This is esp. important if you imagine the route returning a page with relative links. This link
Would point to
/bar
if you're coming from/foo
, to/foo/bar
if you're coming from/foo/
.You can use the following syntax to define a route matching both:
Or the Regexp version mentioned in the comments above.
它们是不同的路线。第二个是带有目录扩展名('/')的 URL;第一个是没有扩展名的 URL。许多框架(如 Rails)会将两者解释为相同的路由,或附加“/”(例如,Django 和 Apache 也可以配置为执行此操作),但从技术上讲,它们是不同的 URL。
They are different routes. The second is a URL with a directory extension ('/'); the first is a URL with no extension. A lot of frameworks (like Rails) will interpret both as the same route, or append the `/' (e.g., Django, and Apache can be configured to do that as well), but technically they are different URLs.