c# - 不使用最小文件的 MVC4 样式捆绑

标签 c# asp.net-mvc

我有 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 '.'

所以我有两个问题:

  1. 根据在线文档,为什么它不起作用?所有信息都说如果发布版本可用,它会选择最小文件?
  2. 如何根据文档使其工作?

最佳答案

我遇到了与您完全相同的问题,我的解决方案包含 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.

事实证明,这就是我的问题的答案。通过将我的 ScriptBundleStyleBundle 都设置为仅键入 Bundle,我现在可以在不进行最小化的情况下获得正确的捆绑。

在调试中,我所有的常规 css/jss 文件都是单独加载的。当我将它设置为非调试时,所有内容都被捆绑,它会自动选择所有 .min 文件。

关于c# - 不使用最小文件的 MVC4 样式捆绑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12971663/

相关文章:

c# - 基于深度 JSON 数据更新 EF 实体

c# - 在 C# 中,按最后一个元素对数组进行排序

c# - 更新到数据库 : Oracle 时出错

ASP.NET MVC - 在生产中更新预编译的 Razor View 文件

javascript - 分页时 MVC 持久模型

c# - 捕获异常处理

c# - 关于GAC中DLL安装的问题

asp.net - PartialView $ ('#webgrid' ).html(data) 导致 webgrid col 单击仅显示网格且不带 css?

c# - ASP.Net Core MVC Identity - 添加临时( session )声明

c# - 模拟 GetExternalLoginInfoAsync() 方法