简体   繁体   English

ASP.NET MVC-捆绑配置顺序

[英]ASP.NET MVC - Bundle Config order

I'm trying to use a specific locale (es-CL) in my ASP.NET MVC 5 application. 我正在尝试在ASP.NET MVC 5应用程序中使用特定的语言环境(es-CL)。 I've the following: 我有以下内容:

  1. Changed web.config uiculture and culture to "es-CL" 将web.config uiculture和区域性更改为“ es-CL”
  2. Installed the Globalize and jQuery.Validation.Globalize packages 安装了GlobalizejQuery.Validation.Globalize
  3. Changed the default language in my views: <html lang="es-cl"> 更改了我视图中的默认语言: <html lang="es-cl">
  4. Created a new Bundle and included in the appropriate views. 创建了一个新的捆绑包,并将其包含在适当的视图中。

In BundleConfig.cs : BundleConfig.cs中

bundles.Add(new ScriptBundle("~/bundles/jqueryval")
    .Include("~/Scripts/jquery.validate.js")
    .Include("~/Scripts/jquery.validate.unobtrusive.js"));

bundles.Add(new ScriptBundle("~/bundles/globalization")
    .Include("~/Scripts/globalize/globalize.js")
    .Include("~/Scripts/globalize/cultures/globalize.culture.es-CL.js")
    .Include("~/Scripts/jquery.validate.globalize.js"));

In the appropriate views: 在适当的视图中:

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
    @Scripts.Render("~/bundles/globalization")
}

However, the generated source code is the following: 但是,生成的源代码如下:

<script src="/Scripts/jquery.validate.js"></script>
<script src="/Scripts/jquery.validate.unobtrusive.js"></script>

<script src="/Scripts/jquery.validate.globalize.js"></script>
<script src="/Scripts/globalize/globalize.js"></script>
<script src="/Scripts/globalize/cultures/globalize.culture.es-CL.js"></script>

Please note that the jquery.validate.globalize.js script is being loaded before globalize.js , which is not what I want. 请注意, jquery.validate.globalize.js脚本是在globalize.js之前加载的,这不是我想要的。

Why is this happening? 为什么会这样呢? Is it possible to rely in the include order in a single bundle, or am I forced to put this single script in a different bundle and specify it in my view? 是否可以依靠单个捆绑软件中的包含顺序,还是我被迫将此单个脚本放入另一个捆绑软件中并在我的视图中指定它?

By default, bundling order is alphabetical for names with wildcards (as pointed out in the comments). 默认情况下,带通配符的名称的绑定顺序是字母顺序的(如注释中所指出)。 However, it also orders based on what it thinks your dependency tree is, and jQuery scripts seem to get slotted to the top. 但是,它也根据它认为您的依赖关系树的顺序进行排序,并且jQuery脚本似乎排在最前面。 You need to create an object that implement IBundleOrder : 您需要创建一个实现IBundleOrder的对象:

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

This prevents the default ordering. 这样可以防止默认排序。 Now to use it: 现在使用它:

var bundle = new ScriptBundle("~/bundles/globalization")
    .Include("~/Scripts/globalize/globalize.js")
    .Include("~/Scripts/globalize/cultures/globalize.culture.es-CL.js")
    .Include("~/Scripts/jquery.validate.globalize.js");

bundle.Orderer = new NonOrderingBundleOrderer();

bundles.Add(bundle);

ref: http://stevescodingblog.co.uk/changing-the-ordering-for-single-bundles-in-asp-net-4/ 参考: http : //stevescodingblog.co.uk/changing-the-ordering-for-single-bundles-in-asp-net-4/

For further reading, an answer to MikeSmithDev's question provides further insight into the default ordering for popular script libraries: 为了进一步阅读,MikeSmithDev问题的答案可以进一步了解流行脚本库的默认顺序:

Ordering of Files within a bundle - What are the known libraries? 捆绑包中文件的排序-已知的库是什么?

In the last version of MVC 5 (at october 27 of 2014), yo should use this class instead: 在MVC 5的最新版本(2014年10月27日)中,您应改用此类:

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

And create the bundle like the other response: 然后像其他响应一样创建捆绑包:

var bundle = new ScriptBundle("~/bundles/globalization")
.Include("~/Scripts/globalize/globalize.js")
.Include("~/Scripts/globalize/cultures/globalize.culture.es-CL.js")
.Include("~/Scripts/jquery.validate.globalize.js");

bundle.Orderer = new AsIsBundleOrderer();

bundles.Add(bundle);

To reduce the codes during creating bundles, I suggest you create an extension method. 为了减少创建捆绑包期间的代码 ,建议您创建一个扩展方法。

Require infrastructure classes: 需要基础设施类:

class NonOrderingBundleOrderer : IBundleOrderer
{
    public IEnumerable<BundleFile> OrderFiles(BundleContext context, IEnumerable<BundleFile> files)
    {
        return files;
    }
}


static class BundleExtentions
{
    public static Bundle NonOrdering(this Bundle bundle)
    {
        bundle.Orderer=new NonOrderingBundleOrderer();
        return bundle;
    }
}

Now simply use it like this: 现在只需像这样使用它:

All in one command 😎 多合一命令😎

bundles.Add(new ScriptBundle("~/bundles/jqueryval")
               .NonOrdering()
               .Include(
                    "~/Scripts/globalize/globalize.js",
                    "~/Scripts/globalize/cultures/globalize.culture.es-CL.js",
                    //...
                );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM