如何在异步加载中订购MVC Bundles?

时间:2016-11-05 10:41:26

标签: c# asp.net-mvc bundle

我按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>

如您所见,在正文的开头,要加载的脚本 async

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

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

@section scripts
{
    @Scripts.Render("~/bundles/contact")
}

<div>
   Contact Contents
</div>

现在,尽管部分脚本已加载到正文的末尾。但由于资源的异步加载,先前加载的依赖脚本(因为它比布局脚本更紧凑)并导致错误!

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

1 个答案:

答案 0 :(得分:4)

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

例如:

此路径中的类BundleConfig 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";
                    }
        }
}

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

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