Dealing with hashed filenames in ASP.Net MVC

21 Feb 2018

This is an extended part of article DEALING WITH HASHED FILENAMES in .net core. Kindly go through the linked article to know more on the background detail for this requirement.

This article primarily focuses on how to deal with the hashed filenames in production builds and non-hashed ones on development machine.

  1. Create a key in your web.config to identify if you want the bundles with minification and hased version numbers.
    <!-- This should be transformed to true in production i.e., web.release.config ->
    <add key="enableHash" value="false" />
  2. Validate the appSettings in the Controller action and set it to view model. 
    model.enableHash = Configuration.AppSettings["enableHash"]
       .equals("true", StringComparision.CurrentCultureIgnorecase);
  3. In the Bundle configuration, include the script files to render as follows
    private static void AddJavaScript(BundleCollection bundles)
       Bundle vendorScript = new ScriptBundle("~/vendors").
  4. In your layout.cshtml, you can check the application setting value (enableHash) and link the appropriate file.
       <!-- this renders the specified bundle with digital fingerprint/hashed version ->
       @Scripts.Render(new string[] { "~/vendors" });
    } else {
       <!- Render non-hashed version that will work with HMR module ->
       <script type="text/javascript" src="/vendor.js"></script>

Above change allows the Hot Module replace to happen normally when appSetting enableHash="false"