我有 4 个文件:
- a.css
- a.min.css
- b.css
- b.min.css
它们以下列方式添加到包中:
bundles.Add(new StyleBundle("~/Content/acss").Include("~/Content/a.css", "~/Content/b.css"));
在调试中运行应用程序时,所有内容都正确呈现:
<link href="/Content/a.css" rel="stylesheet"/>
<link href="/Content/b.css" rel="stylesheet"/>
然而,当在发布中运行时,我以下列方式呈现它:
<link href="/Content/acss?v=mUdXE7_fXKjICzE_XteIB1Igy6TekX1QFh-BtY6fFUw1" rel="stylesheet"/>
我在里面创立:
/* Minification failed. Returning unminified contents. (24,708): run-time error CSS1030: Expected identifier, found '.'
所以我有两个问题:
- 根据在线文档,为什么它不起作用?所有信息都说如果发布版本可用,它会选择最小文件?
- 如何根据文档使其工作?
最佳答案
我遇到了与您完全相同的问题,我的解决方案包含 css/js 文件以及我使用 Web Essentials 最小化的 .min 文件。
如果我在 Debug模式下使用这些包,一切都会正常工作,所有单独的非最小化文件都会加载到我的应用程序中。但是,如果我设置 BundleTable.EnableOptimizations = true;
那么我会收到错误消息,因为它无法最小化我的文件。
基于 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. In a release configuration, jquery-1.7.1.min.js will be added. The bundling framework follows several common conventions such as:
Selecting “.min” file for release when “FileX.min.js” and “FileX.js” exist. Selecting the non “.min” version for debug.
我希望它能简单地加载我已经最小化的文件并将它们捆绑在一起。我认为隐含但在文档中缺失的是,它还会再次最小化我已经最小化的文件,这些文件不起作用并导致输出错误。
我找到了 http://aspnetoptimization.codeplex.com/workitem/56其中提到:
You can skip minification simply by creating bundles with no transform, i.e. don't create ScriptBundles, just normal Bundles.
事实证明,这就是我的问题的答案。通过将我的 ScriptBundle
和 StyleBundle
都设置为仅键入 Bundle
,我现在可以在不进行最小化的情况下获得正确的捆绑。
在调试中,我所有的常规 css/jss 文件都是单独加载的。当我将它设置为非调试时,所有内容都被捆绑,它会自动选择所有 .min 文件。
关于c# - 不使用最小文件的 MVC4 样式捆绑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12971663/