4

Is there any way we can make the ASP.NET 4.5 Bundling functionality generate GUID's as part of the querystring when running in debug mode (e.g bundling turned OFF).

The problem is when developing locally, the scripts/CSS files are generated like this:

<script type="text/javascript" src="/Content/Scripts/myscript.js" /> 

So if i change that file, i need to do a hard-refresh (sometimes a few times) to get the file to be picked up by the browser - annoying.

Is there any way we can make it render out like this:

<script type="text/javascript" src="/Content/Scripts/myscript.js?v=x" /> 

Where x is a GUID (e.g always unique).

Ideas?

I'm on ASP.NET MVC 4.

3 Answers 3

4

Until the NuGet package is patched as per the other answer above, for now i've ended up using the same wrapper code i did for the beta NuGet package:

private static IHtmlString JsUnbundled(this HtmlHelper htmlHelper, string bundlePath) { var jsBuilder = new StringBuilder(); foreach (var file in BundleResolver.Current.GetBundleContents(bundlePath)) { var tagBuilder = new TagBuilder("script"); tagBuilder.Attributes["src"] = file.AddCacheKey(); // add GUID tagBuilder.Attributes["type"] = "text/javascript"; jsBuilder.AppendLine(tagBuilder.ToString()); } return MvcHtmlString.Create(jsBuilder.ToString()); } 

I then have another HTML helper which checks if debug, then uses the above - otherwises uses Scripts.Render.

Obviously this doesn't do any kind of hashing of the file - it will ALWAYS request the file. But i don't mind this, as it's only for debug.

Sign up to request clarification or add additional context in comments.

Comments

1

We don't currently examine the contents of the files in debug mode, but we could add this feature.

I filed it as an issue on codeplex here.

2 Comments

Okay thanks. Guess for now ill wrap an IF DEBUG conditional around rendering out the file, then add a cache key.
Was this feature ever implemented? If so how do you turn it on?
0

Try HashCache: https://github.com/kemmis/System.Web.Optimization.HashCache

Execute the ApplyHashCache() extension method on the BundlesCollection Instance after all bundles have been added to the collection.

BundleTable.Bundles.ApplyHashCache(); 

This will add content hashes to the script/style tags output when in debug mode.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.