捆绑包渲染顺序与包含中的不同

时间:2017-05-24 08:08:50

标签: c# asp.net-mvc bundle

我使用ASP MVC应用程序配置了以下软件包。

bundles.Add(new ScriptBundle("~/Scripts/Core").Include(
                "~/Content/th/assets/global/plugins/jquery.min.js", // Should be first
                "~/Content/th/assets/global/plugins/bootstrap/js/bootstrap.min.js",
                "~/Content/th/assets/global/plugins/js.cookie.min.js",
                "~/Content/th/assets/global/plugins/jquery-slimscroll/jquery.slimscroll.min.js",
                "~/Content/th/assets/global/plugins/jquery.blockui.min.js",
                "~/Content/th/assets/global/plugins/jquery-ui/jquery-ui.min.js", // Should be after jquery.min.js
                "~/Content/th/assets/global/plugins/bootstrap-switch/js/bootstrap-switch.min.js",
                "~/Content/th/assets/global/plugins/bootstrap-select/js/bootstrap-select.min.js",
                "~/Content/th/assets/pages/scripts/components-bootstrap-select.js",
                "~/Content/th/assets/pages/scripts/portlet-draggable.min.js",
                "~/Content/th/assets/global/plugins/bootstrap-multiselect/js/bootstrap-multiselect.js",
                "~/Content/th/assets/global/plugins/po/jquery.webui-popover.min.js",
                "~/Content/th/assets/pages/scripts/components-bootstrap-multiselect.min.js",
                "~/Content/th/assets/pages/scripts/components-date-time-pickers.js"

为什么首先输出jquery-ui.min.js?如何在生成javascript错误时阻止此行为?

以下是@Scripts.Render("~/Scripts/Core")

的输出行
<script src="/Content/th/assets/global/plugins/jquery-ui/jquery-ui.min.js"></script>
<script src="/Content/th/assets/global/plugins/jquery.min.js"></script>
<script src="/Content/th/assets/global/plugins/bootstrap/js/bootstrap.min.js"></script>
<script src="/Content/th/assets/global/plugins/js.cookie.min.js"></script>
<script src="/Content/th/assets/global/plugins/jquery-slimscroll/jquery.slimscroll.min.js"></script>
<script src="/Content/th/assets/global/plugins/jquery.blockui.min.js"></script>
<script src="/Content/th/assets/global/plugins/bootstrap-switch/js/bootstrap-switch.min.js"></script>
<script src="/Content/th/assets/global/plugins/bootstrap-select/js/bootstrap-select.min.js"></script>
<script src="/Content/th/assets/pages/scripts/components-bootstrap-select.js"></script>
<script src="/Content/th/assets/pages/scripts/portlet-draggable.min.js"></script>
<script src="/Content/th/assets/global/plugins/bootstrap-multiselect/js/bootstrap-multiselect.js"></script>
<script src="/Content/th/assets/global/plugins/po/jquery.webui-popover.min.js"></script>
<script src="/Content/th/assets/pages/scripts/components-bootstrap-multiselect.min.js"></script>
<script src="/Content/th/assets/pages/scripts/components-date-time-pickers.js"></script>

1 个答案:

答案 0 :(得分:3)

对于MVC5版本,要按照您的预期维护订单,可以使用下面的

internal class AsIsBundleOrderer : IBundleOrderer
{
    public virtual IEnumerable<FileInfo> OrderFiles(BundleContext context, IEnumerable<FileInfo> files)
    {
        return files;
    }
}

internal static class BundleExtensions
{
    public static Bundle ForceOrdered(this Bundle sb)
    {
        sb.Orderer = new AsIsBundleOrderer();
        return sb;
    }
}

在捆绑时,请提及.ForceOrdered()方法按照您指定的顺序显示它们。

bundles.Add(new ScriptBundle("~/Scripts/Core").Include(
                "~/Content/th/assets/global/plugins/jquery.min.js", // Should be first
                "~/Content/th/assets/global/plugins/bootstrap/js/bootstrap.min.js",
                "~/Content/th/assets/global/plugins/js.cookie.min.js",
                "~/Content/th/assets/global/plugins/jquery-slimscroll/jquery.slimscroll.min.js",
                "~/Content/th/assets/global/plugins/jquery.blockui.min.js",
                "~/Content/th/assets/global/plugins/jquery-ui/jquery-ui.min.js", // Should be after jquery.min.js
                "~/Content/th/assets/global/plugins/bootstrap-switch/js/bootstrap-switch.min.js",
                "~/Content/th/assets/global/plugins/bootstrap-select/js/bootstrap-select.min.js",
                "~/Content/th/assets/pages/scripts/components-bootstrap-select.js",
                "~/Content/th/assets/pages/scripts/portlet-draggable.min.js",
                "~/Content/th/assets/global/plugins/bootstrap-multiselect/js/bootstrap-multiselect.js",
                "~/Content/th/assets/global/plugins/po/jquery.webui-popover.min.js",
                "~/Content/th/assets/pages/scripts/components-bootstrap-multiselect.min.js",
                "~/Content/th/assets/pages/scripts/components-date-time-pickers.js")
.ForceOrdered());