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

夙愿已清 提交于 2019-12-21 20:38:53

问题


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

回答1:


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 !



来源:https://stackoverflow.com/questions/14881014/asp-net-mvc-4-bundling-individual-file-urls-in-debug-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!