我知道处理文件/目录路径的正确方法是使用Path.AltDirectorySeparatorChar
,但这适用于html源路径吗?
或者,反斜杠是html所需的路径分隔符吗?我一直使用反斜杠(即<img src="imagesbirthdaysurprise.jpg" />
),但现在我想知道我是否一直在做不正确的事情?
虽然我不知道定律的确切字母,但所有路径分隔符都使用正斜杠。甚至Windows也可以在其API中使用正斜杠作为分隔符。
使用反斜杠可以在windows平台下工作,但是:
- 看起来很奇怪
- 如果你转移到基于*nix的系统,你会死并呕吐
- 可能会破坏一些假定为正斜杠的代码
- 在许多情况下,
会启动转义序列,因此
n
可能会以意外的方式进行转换
"法律条文"是正斜杠('/')是实现这一点的方法。根据RFC 2396,统一资源标识符(URI):通用语法,§3、"URI语法组件":
The URI syntax does not require that the scheme-specific-part have
any general structure or set of semantics which is common among all
URI. However, a subset of URI do share a common syntax for
representing hierarchical relationships within the namespace. This
"generic URI" syntax consists of a sequence of four main components:
<scheme>://<authority><path>?<query>
each of which, except <scheme>, may be absent from a particular URI.
For example, some URI schemes do not allow an <authority> component,
and others do not use a <query> component.
absoluteURI = scheme ":" ( hier_part | opaque_part )
URI that are hierarchical in nature use the slash "/" character for
separating hierarchical components. For some file systems, a "/"
character (used to denote the hierarchical structure of a URI) is the
delimiter used to construct a file name hierarchy, and thus the URI
path will look similar to a file pathname. This does NOT imply that
the resource is a file or that the URI maps to an actual filesystem
pathname.
hier_part = ( net_path | abs_path ) [ "?" query ]
net_path = "//" authority [ abs_path ]
abs_path = "/" path_segments
URI that do not make use of the slash "/" character for separating
hierarchical components are considered opaque by the generic URI
parser.
opaque_part = uric_no_slash *uric
uric_no_slash = unreserved | escaped | ";" | "?" | ":" | "@" |
"&" | "=" | "+" | "$" | ","
We use the term <path> to refer to both the <abs_path> and
<opaque_part> constructs, since they are mutually exclusive for any
given URI and can be parsed as a single component.
您的URI需要最少数量的前向斜杠来将方案与权限分离。URI的其余部分(路径和查询组件)实际上只有在相关方案以及权限的上下文中才有意义。然而,有几件事需要考虑:
首先,URI路径是而不是文件系统路径(除非URI方案是
file
(file://...
)。URI路径只是对权限有意义的标识符,可能(也可能不)映射到特定的文件系统条目。两个不透明的URI路径(不使用
'/'
作为路径分隔符的路径)不能被通用工具很好地处理。此外,所讨论的路径不一定是不透明的:这意味着将通用工具应用于不透明的URI可能会导致意外行为。