【问题标题】:ASP.Net MVC Style Bundle Not Including Most FilesASP.Net MVC 样式包不包括大多数文件
【发布时间】:2013-02-04 14:17:23
【问题描述】:

最近,我在本地复制的一个项目完全失去了大部分样式。我花了一段时间才弄明白,因为大部分样式都是在一个文件中完成的,其余的是像 Kendo 和 jQuery UI 这样的小东西。

其他次要内容没有添加到页面中。我认为样式已被另一位开发人员修改(有一段时间没有接触这个项目),他只测试 Web API 的东西而不是 UI,所以他可能会破坏它并且永远不知道,但我找到了问题:只有site.css 文件被包含在包中,其他的都没有。我什至尝试重新排列包中包含的 CSS 文件的顺序,它只会包含 site.css

我重建了项目,清除了缓存等,所以它肯定看到了变化。我记得最近更新了一些 NuGet 包或 VS 包之类的东西——甚至可能是 MVC 包?

我的问题是:发生这种情况的原因是否发生了变化?这是什么原因造成的?

编辑:来自BundleConfig.cs的代码:

public static void RegisterBundles(BundleCollection bundles)
{
    bundles.Add(new StyleBundle("~/Content/css").Include(
            "~/Content/site.css",
            "~/Content/themes/kendo/kendo.common.min.css",
            "~/Content/themes/kendo/kendo.default.min.css",
            "~/Content/themes/base/minified/jquery.ui.core.min.css",
            "~/Content/themes/base/minified/jquery.ui.resizable.min.css",
            "~/Content/themes/base/minified/jquery.ui.selectable.min.css",
            "~/Content/themes/base/minified/jquery.ui.accordion.min.css",
            "~/Content/themes/base/minified/jquery.ui.autocomplete.min.css",
            "~/Content/themes/base/minified/jquery.ui.button.min.css",
            "~/Content/themes/base/minified/jquery.ui.dialog.min.css",
            "~/Content/themes/base/minified/jquery.ui.slider.min.css",
            "~/Content/themes/base/minified/jquery.ui.tabs.min.css",
            "~/Content/themes/base/minified/jquery.ui.datepicker.min.css",
            "~/Content/themes/base/minified/jquery.ui.progressbar.min.css",
            "~/Content/themes/base/minified/jquery.ui.theme.min.css"));
}

来自_Layout.cshtml的代码:

@Styles.Render("~/Content/themes/base/css", "~/Content/css")

【问题讨论】:

  • 请您发布您的捆绑代码。
  • 已更新代码,@ngm。
  • 现在不是很有趣:超过 1,000 次观看和问题得分为零...

标签: asp.net-mvc bundling-and-minification


【解决方案1】:

默认情况下,名称以“.min.css”结尾的文件只会包含在 release 版本中。

推荐的捆绑配置是只包含非缩小的 .css 和 .js 文件,然后在发布版本中会自动选择 .min 版本(如果存在),即 Web 中的<compilation debug="false">。配置

您可以通过清除然后将忽略规则添加到BundleCollection.IgnoreList 来控制此行为。 BundleConfig 的示例可能如下所示:

public static class BundleConfig
{
    public static void RegisterBundles(BundleCollection bundles)
    {
        ConfigureIgnoreList(bundles.IgnoreList);

        // Setup your bundles...
    }

    public static void ConfigureIgnoreList(IgnoreList ignoreList)
    {
        if (ignoreList == null) throw new ArgumentNullException("ignoreList");

        ignoreList.Clear(); // Clear the list, then add the new patterns.

        ignoreList.Ignore("*.intellisense.js");
        ignoreList.Ignore("*-vsdoc.js");
        ignoreList.Ignore("*.debug.js", OptimizationMode.WhenEnabled);
        // ignoreList.Ignore("*.min.js", OptimizationMode.WhenDisabled);
        ignoreList.Ignore("*.min.css", OptimizationMode.WhenDisabled);
    }
}

您还可以通过设置BundleTable.EnableOptimizations 来显式启用/禁用优化。

【讨论】:

    猜你喜欢
    • 2010-09-19
    • 1970-01-01
    • 1970-01-01
    • 2011-08-06
    • 2013-06-06
    • 1970-01-01
    • 2012-01-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多