tags:

views:

36

answers:

3

Hi,

What is the correct way to absolute reference an external JavaScript file from the Site.Master file. In Web Forms I used to do it like this src="<%#ResolveUrl(@"Scripts/Common.js") %>".

Thanks

+2  A: 

Use the Content URL helper

<script type="text/javascript" src="<%= Url.Content("~/Scripts/Common.js") %>"></script>
Phil Brown
Thanks, this is exactly what I was looking for.
John McCloskey
A: 

If you're using MVC2 futures assembly you can make use of the Html.Script(string, [string]); method.

<%=Html.Script("/path/to/release/file.js", "path/to/debug/file.js")%>

The second string parameter is optional and allows you to specify a different file to be included when running the project in a Debug configuration.

Nathan Taylor
+1  A: 

I like to use a helper class for this, you can modify it to suit a script reference outside of your site;

public static string JavaScript(this UrlHelper helper, string fileName)
{
    return helper.Content(String.Format("~/Scripts/{0}", fileName));
}

Then in the master page;

<script src="<%=Url.JavaScript("jquery-1.4.2.min.js")%>" type="text/javascript"></script>

Although the futures assembly approach is probably the better way to go now, I like the helper class because it can hold other methods in there that let you find your way to images, css and ClientBin etc...

macou