如何在异步加载中订购MVC bundle

本文关键字:MVC bundle 加载 异步 | 更新日期: 2023-09-27 18:16:30

我用MVC 4设计了一个网站。现在,我遇到了资源加载和脚本执行的时间问题。因为,具有较高优先级的库(如jQuery)应该在第一个运行,但在其他库之后加载,并且脚本依赖于较早加载和运行的库会导致错误!

我的脚本包是:

bundles.Add(new ScriptBundle("~/bundles/scripts").Include(
                           "~/Scripts/jquery-{version}.js",
                           "~/Scripts/jquery-migrate.min.js",
                           "~/Scripts/jquery.unobtrusive-ajax.min.js",
                           "~/Scripts/jquery.knob.min.js",
                           "~/Scripts/toastr.min.js",
                           "~/Scripts/bootstrap.min.js",
                           "~/Scripts/respond.js",
                           "~/Scripts/bootstrap-select.min.js",
                           "~/Scripts/smoothscrool.js", 
                           "~/Scripts/scrollReveal.js",
                           "~/Scripts/easing.min.js",
                           "~/Scripts/site.js"));

bundles.Add(new ScriptBundle("~/bundles/contact").Include(
            "~/Scripts/contact.js"));

这是我的布局:

<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
    @Scripts.RenderFormat("<script type='"text/javascript'" src='"{0}'" async></script>", "~/bundles/scripts")
    <div id="page-content">        
        @RenderBody()
    </div>
    @RenderSection("scripts", required: false)
</body>
</html>

可以看到,在body的开头,要加载的脚本async:

@Scripts.RenderFormat("<script type='"text/javascript'" src='"{0}'" async></script>", "~/bundles/scripts")

这是我的联系人视图剃刀代码:

@section scripts
{
    @Scripts.Render("~/bundles/contact")
}
<div>
   Contact Contents
</div>

现在,尽管section脚本是在正文的末尾加载的。但是由于资源的异步加载,依赖脚本加载较早(因为它比布局脚本更紧凑)而导致错误!

如何强制依赖脚本运行后运行所有布局异步脚本!?

如何在异步加载中订购MVC bundle

在layout视图中,你应该添加一个动态的bundle,以便在每个视图从layout调用时添加资源。换句话说,你可以通过在动态bundle类中定义任何视图资源,在一个文件中为每个视图生成脚本bundle和样式bundle。

例如:

类bundlecconfig在此路径App_Start'BundleConfig:

public class BundleConfig
    {
        public static void RegisterBundles(BundleCollection bundles)
        {
            var styles = new string[]
            {
                "~/Content/bootstrap.min.css",
                "~/Content/bootstrap-select.min.css",
                "~/Content/font-awesome.min.css",
                "~/Content/toastr.min.css",
                "~/Content/front.css",
                "~/Content/style.css"
            };
            var zocial = new string[] { "~/Content/zocial.css" };
            var gridmvc = new string[]
            {
                "~/Content/Gridmvc.css",
                "~/Content/gridmvc.datepicker.min.css"
            };
            bundles.Add(new StyleBundle("~/Content/stylesheets").Include(styles));
            bundles.Add(new StyleBundle("~/Content/stylesheets-zocial").Include(styles.Concat(zocial).ToArray()));
            bundles.Add(new StyleBundle("~/Content/stylesheets-gridmvc").Include(styles.Concat(gridmvc).ToArray()));

        }
}

public static class BundleExtensions
{
        public static string GetViewBundleName(this System.Web.Mvc.HtmlHelper helper, BundleType bundleType)
        {
            var controller = helper.ViewContext.RouteData.Values["controller"].ToString();
            var action = helper.ViewContext.RouteData.Values["action"].ToString();
                    switch (controller.ToLower())
                    {
                        case "home":
                            {
                                switch (action.ToLower())
                                {
                                    case "index": return "~/Content/stylesheets-homepage";
                                    default:
                                        return "~/Content/stylesheets";
                                }
                            }
                        case "sitemaps":
                            return "~/Content/stylesheets-zocial";
                        case "blogs":
                            return "~/Content/stylesheets-gridmvc";
                        case "account":
                            return "~/Content/stylesheets-jqueryval";

                        default:
                            return "~/Content/stylesheets";
                    }
        }
}

最后,在布局中,必须添加脚本此模型为async:

Scripts.RenderFormat("<script type='"text/javascript'" src='"{0}'" async></script>", Html.GetViewBundleName(BundleType.Scripts)))