样式表路径中的斜杠 (/) 与波形符斜杠 (~/)
ASP.NET 提供了两种指定样式表路径的方法:
<link href="/common/black_theme/css/style.css" rel="stylesheet"> (this is working)
<link href="~/common/black_theme/css/style.css" rel="stylesheet"> (this is not working)
- 如何解析这些路径?
- 为什么生成的路径不一样?
- 在哪种情况下我应该选择哪一个?
据我所知, ~
代表应用程序的根目录。 “common”是 IIS 中网站根目录(名为 testsite.demo
)下的文件夹。
物理路径:D:\Physicalpath\WarpFirstSite\testsite.demo
“common”文件夹:D:\Physicalpath\WarpFirstSite\testsite.demo\common
ASP.NET offers two ways to specify paths for style sheets:
<link href="/common/black_theme/css/style.css" rel="stylesheet"> (this is working)
<link href="~/common/black_theme/css/style.css" rel="stylesheet"> (this is not working)
- How are these paths resolved?
- Why are the generated paths different?
- Which one should I pick in which case?
As per my knowledge, ~
represents the root directory of the application.
"common" is the folder below the website root (named testsite.demo
) in IIS.
Physical path: D:\Physicalpath\WarpFirstSite\testsite.demo
"common" folder: D:\Physicalpath\WarpFirstSite\testsite.demo\common
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
/
- 站点根~/
- 应用程序的根目录区别在于,如果您的站点是:
并且您有一个应用程序
myapp
:/
将返回站点的根目录 (http://example.com
),~/
将返回应用程序的根目录 (<代码>http://example.com/mydir/)。/
- Site root~/
- Root directory of the applicationThe difference is that if you site is:
And you have an application
myapp
on:/
will return the root of the site (http://example.com
),~/
will return the root of the application (http://example.com/mydir/
).第二个不起作用,因为除了服务器端的 ASP.NET 代码之外,它不能被任何东西识别的路径。而且由于您的链接标记是常规 html 而不是服务器控件,因此它永远不会被处理。
The second won't work because its not a recognised path by anything except asp.net code on the server side. And since your link tag is regular html and not a server control it never gets processed.
如果您在链接标记中添加
runat="server"
那么它将完美地工作......就像这样......
(这也有效)
If you add
runat="server"
in your link tag then it would works perfectly....like this....
(this is also working)