I would like to do the following:
@Scripts.Render("~/bundles/jquery?version=1"])
the version value has to be dynamic and should match the value defined in a cookie.
How can I add this parameter to Scripts.Render ?
I've tried something like that with jQuery but with no luck:
@Scripts.Render("~/bundles/jquery?version=" + $.cookie('version'))
In an ASP.NET MVC project, the BundleConfig class in the App Start folder can be used to generate style or script bundles. This method has a parameter bundle, which is of the type BundleCollection. At the start of the program, all of the bundles created are added to this bundle parameter.
BundleConfig.cscs file present in a default MVC5 application. This file contains RegisterBundles() method which is being called at Application_Start() event by global. asax. cs file. This RegisterBundles() method contains all the bundles created in the application.
cs file by using default FilterConfig. RegisterGlobalFilters() method. The global filters will be applied to all the controller and action methods of an application. The [HandleError] filter is applied globally in the MVC application by default in every MVC application created using Visual Studio, as shown below.
Go to Views -> Shared -> _Layout. cshtml file and add the render code. Make sure to register the custom javascript file after the jquery bundle since we are going to use jquery inside our js file. Otherwise we will get a jquery error and also register this before the script RenderSection.
Replace
@Scripts.Render("~/bundles/jquery?version=1"])
with
@{string version = 1}
@Scripts.RenderFormat("<script type=\"text/javascript\" src=\"{0}?nocache="+ version +"\"></script>", "~/bundles/jquery")
as shown in this post: http://www.jomendez.com/2016/05/26/how-to-avoid-js-files-cache-script-bundle-with-razor/
By default MVC optimisations will automatically add a version parameter to the bundle links for release builds but not for debug. E.g. when you deploy your site, the link to /bundles/modernizr becomes something like /bundles/modernizr?v=inCVuEFe6J4Q07A0AcRsbJic and the JavaScript is minified.
If one of the files in the bundle has changed the parameter changes on next deployment, hence linked files are cached by browsers but reloaded from the server when they have changed in a new release.
For easier debugging optimisations are disabled in debug (= no version parameter added and no minified code). If you want to override this you can set the compilation debug attribute to false in web.config, or you can enable optimizations in code, like so:
public static void RegisterBundles(BundleCollection bundles)
{
bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
"~/Scripts/jquery-{version}.js"));
// Code removed for clarity.
BundleTable.EnableOptimizations = true;
}
For full details see http://www.asp.net/mvc/overview/performance/bundling-and-minification, in particular the sections "Controlling Bundling and Minification" and "Bundle Caching".
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