【问题标题】:Is there a proper way to create an html src path?有没有合适的方法来创建一个 html src 路径?
【发布时间】:2012-08-30 11:36:48
【问题描述】:

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

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

【问题讨论】:

标签: c# html path imagesource path-separator


【解决方案1】:

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

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

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

【讨论】:

  • 更不用说“\”通常表示许多平台上的转义序列的开始。
【解决方案2】:

“法律条文”是正斜杠 ('/') 是这样做的方式。根据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 需要最少数量的正斜杠来将 schemeauthority 分开。 URI 的其余部分(pathquery 组件)只有在相关 schemeauthority。但是,有几点需要考虑:

  • 一,URI pathsnot 文件系统路径(除非 URI 方案是 file (file://...)。URI 路径只是有意义的标识符授权 可能(或可能不)映射到特定文件系统条目。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-18
    • 2013-09-14
    • 1970-01-01
    • 2018-10-21
    • 1970-01-01
    • 1970-01-01
    • 2010-10-30
    相关资源
    最近更新 更多