在asp.net中的样式表路径中斜杠(/)vs tilde斜杠(〜/)

Kya*_*avi 44 asp.net path relative-path stylesheet absolute-path

如何在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)
Run Code Online (Sandbox Code Playgroud)

据我所知〜表示应用程序的根目录"Common"是IIS中网站根目录下的文件夹(名为testsite.demo)

物理路径= D:\Physicalpath\WarpFirstSite\testsite.demo 公共文件夹位置 -D:\Physicalpath\WarpFirstSite\testsite.demo\common

Ode*_*ded 82

  • / - 站点根目录
  • ~/ - 应用程序的根目录

不同之处在于,如果您的网站是:

http://example.com
Run Code Online (Sandbox Code Playgroud)

你有一个应用程序myapp:

http://example.com/mydir/myapp
Run Code Online (Sandbox Code Playgroud)

/将返回site(http://example.com)的根,

~/将返回application(http://example.com/mydir/)的根.

  • @Kyasa - 您需要解释什么? (3认同)

CRi*_*ice 9

第二个将无法工作,因为除了服务器端的asp.net代码之外,它不是任何可识别的路径.由于您的链接标记是常规html而不是服务器控件,因此它永远不会得到处理.


Gau*_*wal 6

如果您添加runat="server"链接标记,那么它将完美地工作....

像这样....

<link href="~/common/black_theme/css/style.css" rel="stylesheet" runat="server"> 
Run Code Online (Sandbox Code Playgroud)

(这也有效)