I usually refer a jQuery using:
<script src='@(Url.Content("~/JavaScript/jquery.maskedinput-1.2.2.js"))' type="text/javascript"></script>
I refer the same Jquery in many Pages. Any change to the static content (name or version number) requires that all views are updated to absorb the change.In order to solve this I tried creating routes in Global.asax for Jquery file like below
routes.MapRoute("jquery.maskedinput.js", "Javascript/jquery.maskedinput-1.2.2.js");
and i tried to call it in cshtml using a Html Helper like below
<script src='@Html.RouteLink("maskedinput","jquery.maskedinput.js") ' type="text/javascript"></script>
I know RouteLink returns a Anchor tag and so the Source is not rightly set for the Jquery to load.
Requirements:
Help me, please.
I use this method for my BundleCollection with System.Web.Optimization see my question, but this still works even with out it.
namespace PROJECT.Classes
{
public class JSBundles
{
public static string Test = "/scripts/test.js";
}
then use it like this:
<script src='@(JSBundles.Test)' type="text/javascript"></script>
which renders
<script src='/scripts/test.js' type="text/javascript"></script>
Note that this requires a reference in your view to PROJECT.Classes or wherever you put it. I've got that setup in my web.config in the views folder.
....
<add namespace="PROJECT.Classes" />
</namespaces>
</pages>
</system.web.webPages.razor>
You could use the popular and awesome RequireJS to inject jquery (and any other js dependancies) using javascript instead of serving them manually
http://requirejs.org/docs/jquery.html provides setup & usage instructions
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With