URL 路径参数用例

发布于 2024-11-25 10:20:19 字数 316 浏览 3 评论 0原文

通常,当我想到 URL 中的参数时,我会想到查询字符串。然而从技术上讲,在路径段中指定参数也是合法的。因此,给定一个像这样的 URL:

http://www.a.com/frisbee/brand

改为这样写是合法的:

http://www.a.com/frisbee; color=red;size=small/brand;test=1

在实践中我从来没有看到过这一点。哪些框架(如果有)使用了它?

我最近正在使用 ASP.NET MVC3,我不确定它是否可以提取这些类型的参数。

Normally when I think of parameters in an URL, I think of the query string. Technically, however, it is also legal to specify parameters in the path segments. Thus given a URL like this:

http://www.a.com/frisbee/brand

It is legal to write this instead:

http://www.a.com/frisbee;color=red;size=small/brand;test=1

In practice I never see this. What frameworks, if any, do make use of this?

I'm working with ASP.NET MVC3 of late, and I'm not sure it can extract these kinds of parameters.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风吹雨成花 2024-12-02 10:20:19

许多现代框架将支持在其 URL 解析系统中将变量指定为路径段的一部分。

Symfony (PHP) 和 Django (Python) 都支持这一点,因为它们支持通过正则表达式从 URL 中提取值。

一个显着的区别是,查询字符串中指定的参数通常可以按任何顺序,因为它们通常会被解析为类似字典的结构。这不适用于路径段中的参数。您当然可以自己将它们解析为字典,但我刚才提到的框架无法帮助您做到这一点。

请注意,从技术上讲,参数的顺序在 HTTP URI 中很重要,即,参数(在路径或查询字符串中)顺序不同的两个 URL 的一致比较必须假设它们可以引用不同的资源。

Many modern framework will support specifying variables as part of a path segment, in their URL-parsing systems.

Symfony (PHP) and Django (Python) would both support this as they support extracting values from URLs via regular expressions.

One significant difference is that parameters specified in the query string can usually be in any order, because they will typically be parsed into a dictionary-like structure. That wouldn't apply to parameters in a path segment. You could of course parse them yourself into a dictionary, but the frameworks I just mentioned won't help you do that.

Note that technically the order of the parameters is significant in an HTTP URI anyway, i.e. a conforming comparison of two URLs where the parameters (in the path or in the query string) were in a different order would have to assume that they could reference a different resource.

想你只要分分秒秒 2024-12-02 10:20:19

当客户端不支持 cookie 时,Java Servlet 容器使用 URL 重写来通过附加路径参数来维护会话状态。 Servlet 规范规定路径参数必须命名为 jsessionid,

例如 http://example.com/servlet_path;jessionid=E60FF3ABD2926AD9AA45513A385E373D

要使其正常工作,您必须小心地始终通过 response.encodeURL( )response.encodeRedirectURL() 以便容器可以添加必要的路径 范围。

将请求映射到 servlet 的规范需要进一步的支持

用于映射到 servlet 的路径是来自请求对象的请求 URL
减去上下文路径和路径参数

Java Servlet Containers uses URL rewriting to maintain session state when the client does not support cookies by appending a path parameter. The servlet specification says the path parameter must be named jsessionid

e.g. http://example.com/servlet_path;jessionid=E60FF3ABD2926AD9AA45513A385E373D

To get this working you must be careful to always pass the URLs you send back to the client through response.encodeURL() or response.encodeRedirectURL() so that the container can add the necessary path parameter.

Further support is demanded by the specification for mapping requests to servlets

The path used for mapping to a servlet is the request URL from the request object
minus the context path and the path parameters

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文