How to clean the cahe in Asp.Net Mvc5

1

I have an application made in Asp.Net mvc5 and every time I make a change, in my css or js files, I have to go clean the browser cache manually, as a developer there is no problem, the problem is when the application in production, I can not be telling users to do the same.

    
asked by alexander arauz 28.11.2018 в 17:57
source

1 answer

3

A very common way is to use a parameter in the url of the file

<script src="/scripts/archivo.js?v=1"></script>

In the final part of the url is added a querystring , when you do a deployment now what you would have to do is change the number (any number or text works), in this way you take advantage of the cache and when you deploy a new version users will not have to manually clean yours, simply reload the page.

That's the basic strategy, but if you use the feature of < strong> union and minification of ASP.NET MVC, the framework handles the parameter automatically for you:

public static void RegisterBundles(BundleCollection bundles)
{
    bundles.Add(new ScriptBundle("~/bundles/jquery")
           .Include("~/Scripts/jquery-{version}.js"));

    // el resto de tus scripts...

    BundleTable.EnableOptimizations = true;
 }

EnableOptimizations is what causes the parameter to be added, the framework calculates the hash of the file, so when the content changes, the parameter changes. In the view you include them for example:

@Scripts.Render("~/bundles/jquery")

The default template of MVC5 includes the example of how to use this framework feature, you can review it there.

    
answered by 28.11.2018 / 19:15
source