简体   繁体   English

MVC4的样式绑定不使用最小文件

[英]Style bundling for MVC4 not using min files

I have 4 files: 我有4个文件:

  • a.css css
  • a.min.css 最小css
  • b.css b.css
  • b.min.css b.min.css

they are added to bundle in following way: 它们通过以下方式添加到捆绑包中:

bundles.Add(new StyleBundle("~/Content/acss").Include("~/Content/a.css", "~/Content/b.css"));

When running application in debug all is rendered properly: 在调试中运行应用程序时,将正确呈现所有内容:

 <link href="/Content/a.css" rel="stylesheet"/>
 <link href="/Content/b.css" rel="stylesheet"/>

However when running in release I have it rendered in following way: 但是,在发行版中运行时,我可以通过以下方式呈现它:

 <link href="/Content/acss?v=mUdXE7_fXKjICzE_XteIB1Igy6TekX1QFh-BtY6fFUw1" rel="stylesheet"/>

And inside I'm founding: 在里面,我正在建立:

/* Minification failed. / *缩小失败。 Returning unminified contents. 返回未缩小的内容。 (24,708): run-time error CSS1030: Expected identifier, found '.' (24,708):运行时错误CSS1030:预期标识符,找到为“。”。

So I have two questions: 所以我有两个问题:

  1. Why it is not working according to docs online? 为什么在线文档无法正常工作? All information says that it chooses min file if available for release version? 所有信息都说,如果可用的发行版是它选择的是min文件?
  2. How to make it working according to documentation? 如何使其按照文档工作?

I had the exact same issue you had, my solution contained css/js files accompanied by their .min files that I had used Web Essentials to minimize. 我遇到了与您完全相同的问题,我的解决方案包含css / js文件以及我使用Web Essentials最小化的.min文件。

If I used the bundles in debug mode, everything would work correctly and all of the individual non-minimized files would be loaded in my app. 如果我在调试模式下使用捆绑软件,则所有程序都将正常运行,并且所有未最小化的文件都将加载到我的应用程序中。 However, if I set BundleTable.EnableOptimizations = true; 但是,如果我设置BundleTable.EnableOptimizations = true; then I would get errors because it had trouble minimizing my files. 那么我会得到错误,因为它在最小化我的文件时遇到了麻烦。

Based on http://www.asp.net/mvc/tutorials/mvc-4/bundling-and-minification where it states: 基于http://www.asp.net/mvc/tutorials/mvc-4/bundling-and-minification ,其中指出:

For ASP.NET MVC 4, this means with a debug configuration, the file jquery-1.7.1.js will be added to the bundle. 对于ASP.NET MVC 4,这意味着使用调试配置,文件jquery-1.7.1.js将添加到捆绑包中。 In a release configuration, jquery-1.7.1.min.js will be added. 在发布配置中,将添加jquery-1.7.1.min.js。 The bundling framework follows several common conventions such as: 捆绑框架遵循几种常见约定,例如:

Selecting “.min” file for release when “FileX.min.js” and “FileX.js” exist. 当存在“ FileX.min.js”和“ FileX.js”时,选择“ .min”文件进行发布。 Selecting the non “.min” version for debug. 选择非“ .min”版本进行调试。

I expected it to simply load up my already minimized files and just bundle them. 我希望它只是加载我已经最小化的文件,然后将它们捆绑在一起。 What I believe is implied, but missing in the documentation, is that it will also again minimize my already minimized files, which wasn't working and causing errors in the output. 我认为这是隐含的,但在文档中缺少的是,它还会再次最小化我已经最小化的文件,这些文件不起作用并导致输出错误。

I found http://aspnetoptimization.codeplex.com/workitem/56 which mentions: 我找到了http://aspnetoptimization.codeplex.com/workitem/56 ,其中提到:

You can skip minification simply by creating bundles with no transform, ie don't create ScriptBundles, just normal Bundles. 您可以简单地通过创建不带转换的包来跳过最小化,即不创建ScriptBundles,而仅创建普通包。

This turned out to be the answer to my issue. 事实证明,这就是我的问题的答案。 By setting both my ScriptBundle and StyleBundle to just type Bundle , I now get the correct bundling without the minimizing. 通过将ScriptBundleStyleBundle都设置为类型Bundle ,我现在可以在不最小化的情况下获得正确的捆绑。

In debug, all of my regular css/jss files are loaded indidivually. 在调试中,我的所有常规css / jss文件都是单独加载的。 When I set it to non-debug, everything is bundled and it automatically chooses all of the .min files. 当我将其设置为非调试时,所有内容都捆绑在一起,它会自动选择所有.min文件。

Your actual question is not answerable because it is already acting as documentation states (which is why I asked what documentation you were looking it, but you have chosen to ignore my comment). 您的实际问题无法回答,因为它已经充当了文档说明的角色(这就是为什么我问您正在查找的文档,但是您选择忽略我的评论的原因)。

If you want to keep the same behavior in release mode, use BundleTable.EnableOptimizations = false; 如果要在发布模式下保持相同的行为,请使用BundleTable.EnableOptimizations = false; in your Global.asax . 在您的Global.asax This will turn off the bundling and minification that is active in debug mode only . 这将关闭仅在调试模式下处于活动状态的捆绑和最小化。 Then to use your own minified css / js files, simply point your bundles path to the minified versions. 然后,要使用您自己的缩小的css / js文件,只需将包路径指向缩小的版本。

According to documentation, you could also use "~/Content/a{version}.css" which would use non-minified version in debug mode and minified version for release. 根据文档,您还可以使用"~/Content/a{version}.css" ,它将在调试模式下使用非最小版本,而最小版本用于发行。 However, I have not tried that. 但是,我还没有尝试过。

Have you looked a Bundle Transformer , it's 1.6.5 version introduced a property called usePreMinifiedFiles which enables/disables usage of pre-minified files. 您是否看过Bundle Transformer ,它的1.6.5版本引入了一个称为usePreMinifiedFiles的属性,该属性启用/禁用预缩小文件的使用。

Have you set BundleTable.EnableOptimizations = true; 您是否设置了BundleTable.EnableOptimizations = true;

From the documentation 文档中

Note: Unless EnableOptimizations is true or the debug attribute in the compilation Element in the Web.config file is set to false, files will not be bundled or minified. 注意:除非EnableOptimizations为true或将Web.config文件中的complement的debug属性设置为false,否则文件将不会被捆绑或缩小。 Additionally, the .min version of files will not be used, the full debug versions will be selected. 此外,将不使用文件的.min版本,将选择完整的调试版本。 EnableOptimizations overrides the debug attribute in the compilation Element in the Web.config file EnableOptimizations会覆盖Web.config文件中编译元素中的debug属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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