Search code examples
asp.net-mvc-4asp.net-optimization

How does a bundle (Sytem.Web.Optimization) generate the build fingerprint for bundle-links?


In MVC-4, bundles for compressing CSS & Js files can be linked in a layout file with this Razor syntax: @Scripts.Render("~/JavaScripts")

This generates a link in the layout file that includes a fingerprint, which is re-generated on each app-build, to assist with cache-control.

So the generated link looks like:

<script src="/JavaScripts?v=dSMc_JTHMMP5GrWnILSYt_QBMw-g1pPlzknSorXpkyQ1"></script>

I'd like to know how the fingerprint is being generated (to use for similar purposes), but Sytem.Web.Optimization is not yet open-source.


Solution

  • The fingerprint is generated using a sha256 hash of the bytes from the bundle and then encoded:

                byte[] hash = sha256.ComputeHash(Encoding.Unicode.GetBytes(bundle));
                return HttpServerUtility.UrlTokenEncode(hash);