tags:

views:

963

answers:

2

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.

+5  A: 

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") %>' />
Mitchel Sellers
So I would just do that in an in-line code block in my .aspx file?
Mike C.
Mike - Updated the post with an example
Mitchel Sellers
Additionally I'd use ResolveUrl vs ResolveClientUrl, Client url will work out the path relative to where it THINKS you are right now, ie - it will use ../../ if you are above, the ResolveUrl version will always do an absolute path. This may not appear important, but there is a bug regarding *cont*
meandmycode
..how PathInfo isn't considered to be part of the path by the IUrlResolutionService, but the client browser will not see any difference between the path and PathInfo part.
meandmycode
I changed code to:link href="<%= Server.ResolveUrl("~/common/css/global.css") %>"
Mike C.
Am getting error:error CS1061: 'System.Web.HttpServerUtility' does not contain a definition for 'ResolveUrl' and no extension method 'ResolveUrl' accepting a first argument of type 'System.Web.HttpServerUtility' could be found (are you missing a using directive or an assembly reference?)
Mike C.
Hi Mike, use Page.ResolveUrl
meandmycode
I'm getting File Not Found now. I'm going to have to look into this more.
Mike C.
I am unable to make this work. Every value I try for a ResolveUrl parameter yields File Not Found.
Mike C.
Is your website in a folder? does the generated link look like: /common/css/global.css?
meandmycode
My production website lies in a virtual directory, so I configured a virtual path in Visual Studio. I can't even see the generated link because it's throwing an exception.
Mike C.
strange, just stick with the Client one, as long as you don't make use of PathInfo you'll be fine.
meandmycode
@meandmycode: I revisited this today with a fresh mind and your solution works perfectly. Thanks a ton!
Mike C.
+2  A: 

You can make the <link> tag to run at server so Asp.Net will resolve the url for you like this:

<link href="~/common/css/global.css" runat="server" />

(Notice the '~')
I don't know if it can be appied to the <script> tag though, you sould try...

EDIT: I discovered recently on a project that you can (and should) use a ScriptManager to hold your scripts (you can only have 1 per page). You can put one in your MasterPage and reference all your scripts. Inside your content page, you then add a ScriptManagerProxy that will 'reference' the scripts on the master page and you can even add other scripts for that content page only.

Julien Poulin
The tilde will not work for a javascript file.
Mike C.