Slash (/) vs tilde slash (~/) in style sheet path

46,434

Solution 1

  • / - 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/).

Solution 2

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.

Solution 3

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)

Share:
46,434
Kyasa Madhavi
Author by

Kyasa Madhavi

Updated on July 09, 2022

Comments

  • Kyasa Madhavi
    Kyasa Madhavi almost 2 years

    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

  • Oded
    Oded almost 13 years
    @Kyasa - What do you need explaining?
  • Kyasa Madhavi
    Kyasa Madhavi almost 13 years
    i am confused with appliation path . Is it physical path that site example.com mapped to ?
  • Oded
    Oded almost 13 years
    @Kyasa - It is the IIS virtual application. See here - learn.iis.net/page.aspx/150/…
  • Kyasa Madhavi
    Kyasa Madhavi almost 13 years
    Nice link.So we can use ~ in link tag without runat=server tag?
  • Oded
    Oded almost 13 years
    @Kyasa - No, this is a server side feature.
  • Kyasa Madhavi
    Kyasa Madhavi almost 13 years
    Hi Oded. The concept you explain is right. But it is working for image src , but not for importing stylesheet using Link tag.Unable to use tilde in link tag
  • Oded
    Oded almost 13 years
    @Kyasa - Is it a server side element?
  • Kyasa Madhavi
    Kyasa Madhavi almost 13 years
    yes it is serverside < link href="~/Images/simple2.css" id="testcss" runat="server" type="text/css" /> and <img id="Img2" runat="server" src="~/Images/aaa.png" /> Both images and css are at same location.
  • Anibal Díaz
    Anibal Díaz over 7 years
    This don't work. I apply this solution, and the render code with folder of page, no with folder of web application. example: <link href="~/CSS/MyStyle.css" rel="StyleSheet" runat="server" /> result: localhost/AppFolder/OtherFolder/~/CSS/MyStyle.css Css folder is under AppFolder.