简体   繁体   中英

ASP.NET MVC 4 Bundling - Individual File URLs in DEBUG mode

Problem:

In the HTML5 offline app being done in ASP.NET MVC 4.5, we are bundling and minifying the styles and scripts using the framework's built-in feature . Everything working well with the pages themselves, but for writing into the Cache Manifest, where (because of the we we are writing it) it is always only emitting the bundled URL.

And so, we are not able to debug JavaScript in the offline mode, as the individual debug js files are not getting into the application cache.

Code:

RegisterBundles

This is how our BundleConfig.RegisterBundles look:

    // For more information on Bundling, visit http://go.microsoft.com/fwlink/?LinkId=254725
    public static void RegisterBundles(BundleCollection bundles)
    {
        bundles.Add(new ScriptBundle("~/bundles/Scripts").Include(
                   "~/Scripts/*.js"
                   ));
    }

HTML Markup

And we include it in our _Layout.cshtml for the pages itself like this:

@System.Web.Optimization.Scripts.Render("~/bundles/Scripts")

This works well for the pages, by emitting the individual js files when debug is true , and one bundled file when debug is false .

Output in debug=true

<script src="/Scripts/ScriptOne.js"></script>
<script src="/Scripts/ScriptTwo.js"></script>
<script src="/Scripts/ScriptThree.js"></script>

Output in debug=false

<script src="/bundles/Scripts?v=B0_RvAM_5ifnREcGnNQ3FO8qQp4vyLOdtCUJ-2mXSuA1"></script>

Cache-Manifest

And this is how we include the scripts into our CacheManifest

@System.Web.Optimization.BundleTable.Bundles.ResolveBundleUrl("~/bundles/Scripts")

Output in debug=true and debug=false

/bundles/Scripts?v=B0_RvAM_5ifnREcGnNQ3FO8qQp4vyLOdtCUJ-2mXSuA1

What we want?

We would like to know if there is a way we could get the Cache-Manifest to be output like this:

Output in debug=true

/Scripts/ScriptOne.js
/Scripts/ScriptTwo.js
/Scripts/ScriptThree.js

Output in debug=false

/bundles/Scripts?v=B0_RvAM_5ifnREcGnNQ3FO8qQp4vyLOdtCUJ-2mXSuA1

The MSDN documentation talks about a certain Scripts.RenderFormat method, which looked like a good candidate for doing what we want. But intellisense was complaining this RenderFormat method is not present in the version of System.Web.Optimization currently referenced.

But, thanks (once again) to Google, this answer here on SO was explaining that this RenderFormat method is in fact, included in the next version's alpha release. And the comment on that answer linked to the page that explains how we could install it:

PM> Install-Package Microsoft.AspNet.Web.Optimization -Pre

With this version, the Cache-Manifest could be changed to:

@System.Web.Optimization.Scripts.RenderFormat("{0}","~/bundles/Scripts")

And we now have the cache manifest emit the individual files while debug=true .

Apparently, the MSDN documentation is not in sync with the current stable release !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM