链接前缀“~/”之间的区别和“/”

发布于 2024-11-09 13:40:12 字数 800 浏览 0 评论 0原文

有时我们需要将 / 或 ~/ 作为后缀,以便引用根

<script type="text/javascript" src="/scripts/jquery-1.5.1.min.js"></script>
<link href="~/Styles/MasterPage.css" rel="stylesheet" type="text/css" />

这是我从 Phill 那里知道的

~/ 是无效的,除非你的控件/元素上有 runat 属性。 “/”本身指定浏览器从根目录查找,因此如果您从 www.mysite.com/product/view.aspx 链接到“/css/main.css”,它将在 www 中查找 css 文件.mysite.com/css/main.css。当您将“~/”与 runat-server 一起使用时,它将在运行时计算出目录的路径,因此在使用“~/css/main.css”的同一示例中,呈现的 url 将类似于“../css” /main.css',因为它需要在找到目录'css'之前将目录回退1。 (希望这是有道理的)——菲尔 如何在母版页标题中包含 jquery路径问题?

但正如我所尝试的那样,即使我使用 / firefox 仍然引用 ../../ 而不是静态路径。 另外,我不太清楚如何正确使用它,哪个在哪​​种情况下使用。

Sometime we need to put / or ~/ as the suffix in order to make refer to the root

<script type="text/javascript" src="/scripts/jquery-1.5.1.min.js"></script>
<link href="~/Styles/MasterPage.css" rel="stylesheet" type="text/css" />

This is what i know from Phill

~/ is not valid unless you have a runat attribute on your control/element. '/' by itself specifies to the browser to look from the root directory, so if you link to '/css/main.css' from www.mysite.com/product/view.aspx it will look for the css file in www.mysite.com/css/main.css. When you use '~/' with runat-server, it will work out the path to the directory at runtime, so in the same example with '~/css/main.css' the rendered url will look like '../css/main.css' because it needs to drop the directory back 1 before finding the directory 'css'. (hope that makes sense) – Phill
How to include jquery in masterpage header without path problems?

but as i has tried, even i use / firefox still refer to ../../ instead of static path.
also, I don't know it clearly how to use it correctly , which one use on which situation.

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

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

发布评论

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

评论(2

山人契 2024-11-16 13:40:12

~/ 指的是虚拟目录应用程序根目录,其中“/”指的是站点根目录,例如,如果您的站点位于 http:// www.example.com/ 并具有 http://www.example.com/foo 其中文件夹“foo”配置为虚拟目录IIS 中的应用程序 http://www.example.com/foo/foo.aspx< 中的页面/a> 会将 ~/ 解析为相对值到虚拟目录应用程序根目录,即 http://www.example.com/foo 但“/”仍将解析为网站根目录 http://www.example.com/

~/ refers to virtual directory application root where as "/" refers to site roots eg if you have site that in http://www.example.com/ and which has http://www.example.com/foo where folder "foo" configured as virtual directory application in IIS a page in http://www.example.com/foo/foo.aspx will resolve ~/ as relative to virtual directory application root directory which is http://www.example.com/foo but "/" will still resolve to site root which is http://www.example.com/

屋檐 2024-11-16 13:40:12

相对 URL 开头的 / 可追溯到主机名的根。

相对 URL 中的前缀 ~/ 只能由 ASP.NET 进程解释,这就是它仅适用于 runat=server 的原因。它在输出时被翻译为引用 ASP.NET 应用程序的根 URL。很多时候,这与主机名的根目录相同,但如果运行代码的 ASP.NET 应用程序是 IIS 中的虚拟目录而不是其自己的站点,则情况会有所不同。

如果只需要回退一级目录,可以使用“../”作为前缀,而不是“/”或“~/”。

The / at the beginning of a relative URL backs up to the root of the hostname.

The prefix ~/ in a relative URL can only be interpreted by the ASP.NET process, which is why it only works for runat=server. It is translated upon output to refer to the root URL of the ASP.NET application. Many times, this is the same as the root of the hostname, but it will differ if the ASP.NET application that the code is running in is a virtual directory in IIS rather than its own site.

If you only need to drop one directory level back, you can use "../" as the prefix rather than "/" or "~/".

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