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

3

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

Honewort answered 21/6, 2011 at 10:53 Comment(0)
I
103
  • / - 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/).

Individual answered 21/6, 2011 at 10:55 Comment(8)
@Kyasa - What do you need explaining?Individual
i am confused with appliation path . Is it physical path that site example.com mapped to ?Honewort
@Kyasa - It is the IIS virtual application. See here - learn.iis.net/page.aspx/150/…Individual
Nice link.So we can use ~ in link tag without runat=server tag?Honewort
@Kyasa - No, this is a server side feature.Individual
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 tagHonewort
@Kyasa - Is it a server side element?Individual
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.Honewort
B
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.

Bostick answered 21/6, 2011 at 11:5 Comment(0)
T
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)

Try answered 21/6, 2011 at 10:57 Comment(1)
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.Dashed

© 2022 - 2024 — McMap. All rights reserved.