0

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

3 Answers 3

3

Use the Content URL helper

<script type="text/javascript" src="<%= Url.Content("~/Scripts/Common.js") %>"></script>
Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

2

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...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.