Saturday, May 11, 2024
Homepage · c#
 Popular · Latest · Hot · Upcoming
-3
rated 0 times [  3] [ 6]  / answers: 1 / hits: 28259  / 15 Years ago, thu, april 9, 2009, 12:00:00

I have a Master Page in the root of my project. I have Content Pages throughout my project and in subfolders referencing this Master Page. What is the correct way to reference my .CSS and .JS files if I always want them to be relative to the root?



Here is how I'm doing it now:




link href=/common/css/global.css
script src=/common/javascript/global.js


But that breaks the link. I tried without the leading / but that didn't work on my pages in the subfolders.


More From » c#

 Answers
12

I would use something like



Server.ResolveClientUrl(~/common/css/global.css)


This will get a proper url for you at all times.



Example:



Per the comment this would be full usage.



<link type=text/css rel=stylesheet 
href='<%= Server.ResolveClientUrl(~/common/css/global.css) %>' />


According to comments, other validated usage, no error CS1061: 'System.Web.HttpServerUtility' does not contain a definition error:



    <script type=text/javascript 
src=<%= Page.ResolveUrl(~/Scripts/YourScript.js) %> ></script>


Also is important to always put the closing tag .


[#99723] Friday, April 3, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
brookelynshaem

Total Points: 468
Total Questions: 98
Total Answers: 101

Location: China
Member since Mon, Aug 22, 2022
2 Years ago
;