如何配置MVC的样式捆绑顺序?

Bif*_*iff 23 asp.net-mvc-4 bundling-and-minification

我的网络应用程序正在使用jquery-ui和jqgrid设置的大图标.
为了在升级jquery-ui或jqgrid时轻松维护对CSS的更改以容纳更大的图标,我有一个单独的CSS文件,我有一堆覆盖.

你可以想象这个覆盖文件必须包含在jquery-ui样式表和jqgrid样式表之后.

我将所有的样式表都放到了这样的包中

bundles.Add(new StyleBundle("~/Content/dark-hive/allstyles").Include(
    "~/Content/dark-hive/jquery-ui-1.8.23.custom.css",
    "~/Content/ui.jqgrid.css",
    "~/Content/jquery-ui-fixes.css",
    "~/Content/icons.css",
    "~/Content/site.css"));
Run Code Online (Sandbox Code Playgroud)

但它正如此呈现!

<link href="/Content/dark-hive/jquery-ui-1.8.23.custom.css" rel="stylesheet"/>
<link href="/Content/jquery-ui-fixes.css" rel="stylesheet"/>
<link href="/Content/ui.jqgrid.css" rel="stylesheet"/>
<link href="/Content/icons.css" rel="stylesheet"/>
<link href="/Content/site.css" rel="stylesheet"/>
Run Code Online (Sandbox Code Playgroud)

如何配置我的包以正确的顺序呈现?

更新
确定,这是愚蠢但它的工作.

无论我做什么,文件总是会错误地呈现.所以我尝试了一些愚蠢的东西,首先添加了jquery-ui-fixes.css,最后添加了jquery-ui-1.8.23.custom.css.

突然我的命令是

<link href="/Content/jquery-ui-fixes.css" rel="stylesheet"/>
<link href="/Content/dark-hive/jquery-ui-1.8.23.custom.css" rel="stylesheet"/>
<link href="/Content/ui.jqgrid.css" rel="stylesheet"/>
<link href="/Content/icons.css" rel="stylesheet"/>
<link href="/Content/site.css" rel="stylesheet"/>
Run Code Online (Sandbox Code Playgroud)

我将我的javascript文件重命名为jqueryuifixes.css,现在它的顺序保存在较低的js文件中.

我想如果一个样式表有一个 - 在名称中它首先由于某种原因被优先排序,并且它的顺序与其他文件一起维护 - 在其中.

如果有人能解释这个,我会给他们支票.

Fen*_*ton 42

如果您单独包含每个文件,捆绑包将尊重您的订单......

var bundle = new Bundle("~/Content/dark-hive/allstyles", new StyleBundle());           
bundle.Include("~/Content/dark-hive/jquery-ui-1.8.23.custom.css");
bundle.Include("~/Content/ui.jqgrid.css");
bundle.Include("~/Content/jquery-ui-fixes.css");
bundle.Include("~/Content/icons.css");
bundle.Include("~/Content/site.css");
bundles.Add(bundle);
Run Code Online (Sandbox Code Playgroud)

UPDATE

即使使用明确的顺序,您也会发现有一个相当方便的内置订购系统,它首先命令特定命名的文件.要完全清除这一点,您可以使用:

bundles.FileSetOrderList.Clear();
Run Code Online (Sandbox Code Playgroud)

您可以使用以下命令添加自己的自定义排序:

BundleFileSetOrdering ordering = new BundleFileSetOrdering("My Order");
ordering.Files.Add("jquery.js");

bundles.FileSetOrderList.Clear();
bundles.FileSetOrderList.Add(ordering);
Run Code Online (Sandbox Code Playgroud)

从本质上讲,有一个庞大的内置文件列表将在任何不在列表中的文件之前按特定顺序放置 - 但这些选项可让您控制.

  • 订单仍然不正确,我想也许我正在使用的优化dll版本存在问题. (2认同)
  • 找到它 - 内置特定顺序的特定文件的大列表 - 但您可以覆盖它.答案已更新. (2认同)

Vla*_*den 10

您可以创建自定义捆绑器并覆盖OrderFiles方法

public class CustomBundleOrderer : IBundleOrderer
{
    public IEnumerable<FileInfo> OrderFiles(BundleContext context, IEnumerable<FileInfo> files)
    {
        return files;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后按照您希望捆绑的顺序传递您的css文件

var bundle = new StyleBundle("~/Content/dark-hive/allstyles")
{
    Orderer = new CustomBundleOrderer()
};

bundle.Include(
    "~/Content/dark-hive/jquery-ui-1.8.23.custom.css",
    "~/Content/ui.jqgrid.css",
    "~/Content/jquery-ui-fixes.css",
    "~/Content/icons.css",
    "~/Content/site.css");

bundles.Add(bundle);
Run Code Online (Sandbox Code Playgroud)