Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp.net mvc 4: adding parameter to Scripts.Render path

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'))
like image 911
Sam Avatar asked Aug 06 '13 08:08

Sam


People also ask

How do I bundle a script in MVC?

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.

What is BundleConfig Cs in MVC?

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.

What is the use of FilterConfig in MVC?

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.

HOW include script in Cshtml?

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.


2 Answers

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/

like image 96
kvarkel Avatar answered Sep 18 '22 14:09

kvarkel


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

like image 36
bgx Avatar answered Sep 18 '22 14:09

bgx