ASP.NET offers two ways to specify paths for style sheets:
<link href="/common/black_theme/css/style.css" rel="nofollow noreferrer noopener" rel="stylesheet"> (this is working) <link href="~/common/black_theme/css/style.css" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" 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:PhysicalpathWarpFirstSitetestsite.demo
“common” folder: D:PhysicalpathWarpFirstSitetestsite.democommon
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 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/).
Method 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.
Method 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="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" rel="stylesheet" runat="server">
(this is also working)
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0