强制 ASP.Net MVC Bundle 按特定顺序呈现 javascript 文件 [英] Force ASP.Net MVC Bundle to render the javascript files in a certain order
问题描述
我正在开发一个 ASP.Net MVC 4 应用程序,并使用 Bundling 和 minifiction 来呈现样式和脚本文件.
I am working on an ASP.Net MVC 4 application and using Bundling and minifiction to render styles and script files.
我有一个脚本文件(文件 A),它在我使用时调用另一个文件(文件 B)中的函数@Scripts.Render()
方法它在 File B 之前为 File A 渲染 link
标签,因此它会触发错误并且脚本无法正常工作.
I have a script file (File A) that call function in another file (File B) , when I am using
@Scripts.Render()
method it render link
tag for File A before File B so it fire an error and the script not work correctly.
有没有办法强制 @Script.Render()
以特定顺序呈现 link
标签,而不用为每个文件使用单独的包????
Is there any way to force @Script.Render()
to render link
tags in certain order without using separate bundle for each file????
编辑
我正在使用 IncludeDirectory
方法来包含该文件夹中的所有脚本文件
I am using IncludeDirectory
method to include all script files in that folder
public class BundleConfig
{
public static void RegisterBundles(BundleCollection bundles)
{
bundles.Add(new ScriptBundle("~/bundles/js").IncludeDirectory(
"~/Scripts/js",
"*.js"));
}
}
推荐答案
将您自己的订购者写为 Darin Dimitrov 回答这个问题:如何指定显式的 ScriptBundle 包含顺序?
Write your own orderer as Darin Dimitrov answer in this question: How can I specify an explicit ScriptBundle include order?
public class MyBundleOrderer : IBundleOrderer
{
public virtual IEnumerable<FileInfo> OrderFiles(BundleContext context, IEnumerable<FileInfo> files)
{
//any ordering logic here
return files;
}
}
这篇关于强制 ASP.Net MVC Bundle 按特定顺序呈现 javascript 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!