I\'m fairly new to ASP.NET MVC, and I\'m having a little trouble with scripts... in particular, I want to use jQuery in most pages, so it makes sense to put it in the master
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?
I just use a slash(/). For Example:
<script src="/Script/jquery-1.4.1.js"></script>
when the "jquery-1.4.1.js" file is in the Script directory of root. And it works perfectly.
I made some of what OJ mentions, I created a GoogleHelper class with this methods
public static string ReferenceGoogleAPI()
{
var appSettings = new AppSettingsReader();
string apiKey = appSettings.GetValue("GoogleApiKey", typeof(string)).ToString();
return ReferenceGoogleAPI(apiKey);
}
public static string ReferenceGoogleAPI(string key)
{
return "<script type=\"text/javascript\" src=\"http://www.google.com/jsapi?key=" + key + "\"></script>";
}
public static string ReferenceGoogleLibrary(string name, string version)
{
return "<script type=\"text/javascript\">google.load(\"" + name + "\", \"" + version + "\");</script>";
}
Now I'm adding extra methods to get some ClientLocation data ;)
You can try Url.Content extension method with Razor syntax
<script src="@Url.Content("~/Scripts/jquery.min.js")" type="text/javascript"></script>
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>