4

我知道处理文件/目录路径的正确方法是使用Path.AltDirectorySeparatorChar,但这是否适用于 html 源路径?

或者,是否反斜杠是 html 所需的路径分隔符?我一直使用反斜杠(即<img src="images\birthdaysurprise.jpg" />)来完成它,但现在我想知道我是否一直在做不正确的事情?

4

2 回答 2

2

虽然我不知道法律的确切字母,但对所有路径分隔符使用正斜杠。甚至 Windows 也可以在其 API 中使用正斜杠作为分隔符。

使用反斜杠可能在 Windows 平台下工作,但是:

  • 看起来很奇怪
  • 如果您迁移到基于 *nix 的系统,将会死而复生。
  • 最终可能会破坏一些假设正斜杠的代码
  • \在许多情况下会启动转义序列,因此\n可能会以意想不到的方式进行翻译。
于 2012-09-05T17:27:00.510 回答
1

“法律条文”是正斜杠('/')是这样做的方式。根据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 可能会导致在意想不到的行为中。

于 2012-09-05T18:06:53.820 回答