55

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

Shashank Shekhar
  • 3,958
  • 2
  • 40
  • 52
Kyasa Madhavi
  • 705
  • 1
  • 6
  • 10

3 Answers3

102
  • / - Site root
  • ~/ - Root directory of the application

The difference is that if you site is:

http://example.com

And you have an application myapp on:

http://example.com/mydir/myapp

/ will return the root of the site (http://example.com),

~/ will return the root of the application (http://example.com/mydir/).

Oded
  • 489,969
  • 99
  • 883
  • 1,009
12

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.

CRice
  • 12,279
  • 7
  • 57
  • 84
8

If you add runat="server" in your link tag then it would works perfectly....

like this....

<link href="~/common/black_theme/css/style.css" rel="stylesheet" runat="server"> 

(this is also working)

Vishal Suthar
  • 17,013
  • 3
  • 59
  • 105
Gaurav Agrawal
  • 4,355
  • 10
  • 42
  • 61
  • 1
    This don't work. I apply this solution, and the render code with folder of page, no with folder of web application. example: result: http://localhost/AppFolder/OtherFolder/~/CSS/MyStyle.css Css folder is under AppFolder. – Anibal Díaz Oct 07 '16 at 08:40