ASP.NET MVC 4 app with bundling and minification, why is minification enabled in debug mode?

牧云@^-^@ 提交于 2019-12-04 22:35:57
Hao Kung

The red flag is with the link/script tags rendered in your HTML:

These should contain a version hashcode if you are using Script/Style.Render, i.e.

< script src="/bundles/jquery?v=wvLq7H7qEZB2giyIRn7aEZAxhHOb2RfTYYh2HMd9EqM1"/>

To get the debug/release behavior that the MVC4 templates are using, you must use the Script/Style.Render methods as well. When calling these methods, you must pass virtual bundle paths, in your example:

@Styles.Render("~/content/css")
@Scripts.Render("~/bundles/jquery")

In debug mode, you should not get link/script tags pointing at the bundle (which will always be minified/bundled). Instead you should be getting script/link tags to the individual resources in debug mode.

I just had this happen on a brand new ASP.NET MVC project. I had the <compilation debug="true" targetFramework="4.5.1" /> set to true in web.config and was still getting minified output.

The fix

BundleConfig.cs (in App_Start) has a line at the bottom BundleTable.EnableOptimizations = true; which was overriding my web.config setting....

Remove the line and/or set it to false and I got my scripts as unminified/unbundled as desired in debug environment.

I recommend removing the line since this will override web.config. Setting this in the web.config has the advantage of using web.config transforms so that you can create different settings for deploying to different environments.

For more information on this see http://www.asp.net/mvc/tutorials/mvc-4/bundling-and-minification and read the Controlling Bundling and Minification section (about half way down the article).

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