Using scripts in a master page with ASP.NET MVC

Solution 1:

I have a AppHelper class with some methods for adding script references:

public static string ReferenceScript(string scriptFile)
{
    var filePath = VirtualPathUtility.ToAbsolute("~/Scripts/" + scriptFile);
    return "<script type=\"text/javascript\" src=\"" + filePath + "\"></script>";
}

so in your master page you can use:

<%= AppHelper.ReferenceScript("jquery-1.2.6.js") %>

Solution 2:

I think the simplest way is to use the following, and it works in views to.

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

</script>

Solution 3:

Based on the other replies, perhaps an extension method on Html (which is very common for MVC), similar to Eduardo's answer:

 <%=Html.Script("~/Scripts/jquery-1.2.6.js")%>

With:

public static string Script(this HtmlHelper html, string path)
{
    var filePath = VirtualPathUtility.ToAbsolute(path);
    return "<script type=\"text/javascript\" src=\"" + filePath + "\"></script>";
}

Solution 4:

Why not just point your master page at Google's js file hosting? Then even when it comes to deployment (assuming your site is Net facing) you can abuse possibly pre-cached jquery files?