不要在ASP.NET MVC 4的BundleConfig中压缩某些文件

20

我不想对我在ASP .NET MVC 4解决方案中使用的所有文件进行缩小。例如,我在我的BundleConfig.cs文件中有以下内容:

bundles.Add(new StyleBundle("~/CSS/bootstrap").Include(
    "~/Content/Bootstrap/body.css",
    "~/Content/Bootstrap/bootstrap-responsive.css",
    "~/Content/Bootstrap/bootstrap-mvc-validation.css",
    "~/Content/Bootstrap/bootstrap-theme.css",
    "~/Content/Bootstrap/bootstrap.css"
    ));

...

当然,为了使其更小,我使用:

BundleTable.EnableOptimizations = true;

它很好用。

但是现在,我该如何缩小所有文件,除了一个包?我有一个删除一些CSS类的包的问题,希望不要缩小这个包。

非常感激任何帮助。


如果你将文件 xxxxx.min.css 重命名,它是否仍然尝试进行缩小? - mxmissile
谢谢你的回答,但我更愿意不这样做。当我更新Bootstrap时,我不想移动和重命名文件。 - Léo Davesne
3个回答

24
使用 Transforms.Clear() 来跳过缩小文件,但保持文件束。
//declare bundle
var bundle = new ScriptBundle("~/javascripts/ckEditor")
                .Include("~/Scripts/ckeditor/ckeditor.js")
                .Include("~/Scripts/ckeditor/config.js");

//skip transformation. Result is that files are only bundled, but not minified.
bundle.Transforms.Clear();

bundles.Add(bundle);

你还需要从目录中删除.min.js文件以防止替换


1
正如这个问题所指出的,只需调用Bundle而不是ScriptBundle - dudeNumber4

5
我遇到了类似的问题。解决方法是在视图中禁用并启用缩小。例如:

我曾经遇到过类似的问题。解决办法是在视图中关闭并重新启用缩小功能。

@{
    BundleTable.EnableOptimizations = false;
 }

@Styles.Render("~/layout")
@RenderSection("CSS", false)

@{
    BundleTable.EnableOptimizations = true;
}

4
线程安全性 = 0。即页面1渲染一些其他的捆绑包,而页面2正在渲染布局。结果是页面1也会获得一个未经压缩的捆绑包。 - John
如同此问题中所述,只需调用Bundle而不是ScriptBundle即可。 - dudeNumber4

4
我不知道问题来自哪里,但我尝试了以下操作:
  • Just bundle, not minify. It doesn't work.

    bundles.Add(new Bundle("~/CSS/bootstrap").Include(
        "~/Content/Bootstrap/body.css",
        "~/Content/Bootstrap/bootstrap-responsive.css",
        "~/Content/Bootstrap/bootstrap-mvc-validation.css",
        "~/Content/Bootstrap/bootstrap-theme.css",
        "~/Content/Bootstrap/bootstrap.css"
        ));
    
  • Override UI errors. It works but it's just a temporary patch.

最后,我决定使用标准的CSS调用(并在代码中添加了一些注释以解释原因):
<link rel="stylesheet" href="/Content/Bootstrap/body.css">
<link rel="stylesheet" href="/Content/Bootstrap/bootstrap-responsive.css">
<link rel="stylesheet" href="/Content/Bootstrap/bootstrap-mvc-validation.css">
<link rel="stylesheet" href="/Content/Bootstrap/bootstrap-theme.css">
<link rel="stylesheet" href="/Content/Bootstrap/bootstrap.css">

如果您有更好的想法,请告诉我们! :)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接