You have to resolve the path "~/assets/css/globalStyles.css" using Page.ResolveUrl
(WebForms) or Url.Content
(MVC) - so your Alt 3 must work. Look at the page html through browser's "View source" - what is href there?
Absolute paths for CSS files in ASP.net
-
13-10-2022 - |
Question
I had an error in my asp website when I deployed it to IIS6 telling me that I cannot use a leading .. to exit above the top directory. I searched around google and viewed similar questions posted here at stackoverflow and saw recommendations to replace my relative paths ... with ~.
Here is the path for my CSS folder:
LMS\assets\css
Where the page is located that tries to access the CSS:
LMS\MasterPages
My project structure:
LMS
-->assets
-->css
-->MasterPages
-->Default.aspx
However, I can't seem to get my paths working. My Default.aspx cannot find my CSS file. Here is an example:
Working:<link href="../assets/css/globalStyles.css" rel="stylesheet" runat="server"/>
Alt 1:href="~/assets/css/globalStyles.css"
Alt 2:href="~\\assets\\css\\globalStyles.css"
Alt 3:href="<%= Page.ResolveUrl("~/assets/css/globalStyles.css") %>"
All of these alternatives don't work. When I debug my site, no styles are present and all are just plain texts. Google Chrome pointed out that my path was being read incorrectly. It was read as:
http://localhost:2981/LMS/Admin/~/assets/css/globalStyles.css
.
Can share some thoughts on why this is happening? Why can't my page find my CSS files? I even tried to bind the Page Header in page load but its still not working.
Some ideas? Any help will be greatly appreciated!
Solution
OTHER TIPS
Either -> Url.Content("~/assets/css/globalStyles.css") Or you can directly use -> href="/assets/css/globalStyles.css"