有没有一种正确的方法来创建一个html src路径?

时间:2012-09-05 17:24:42

标签: c# html path imagesource path-separator

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

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

2 个答案:

答案 0 :(得分:1)

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

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

  • 看起来很奇怪
  • 如果转移到基于* nix的系统,将会死亡和堵嘴。
  • 最终可能会破坏一些假设正斜杠的代码
  • \在许多情况下启动转义序列,因此\n可能会以意想不到的方式转换。

答案 1 :(得分:1)

“法律条文”是正斜杠('/')是这样做的方式。 Per RFC 2396, Uniform Resource Identifiers (URI): Generic Syntax,§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的其余部分(路径查询组件)仅在相关方案权限<的上下文中具有意义/ em>的。但是,有几件事需要考虑:

  • 其中,URI 路径文件系统路径(除非URI方案为filefile://...)。URI路径为只是具有权限含义的标识符可能(或可能不)映射到特定文件系统条目。

  • 两个不透明的URI路径(不使用'/'作为路径分隔符的路径)无法通过通用工具很好地处理。此外,所讨论的路径不一定是不透明的:意味着将通用工具应用于不透明的URI可能会导致意外行为。