捆绑而不是捆绑在ASP.NET MVC中的生产和测试环境中

时间:2013-11-04 18:41:01

标签: c# javascript asp.net-mvc debugging bundling-and-minification

我正在使用ASP.NET Bundling机制:

            BundleTable.Bundles.Add(new ScriptBundle("~/Scripts/Master-js").Include(
                        "~/Scripts/respond.min.js",
                        "~/Scripts/jquery.form.js",
                       "~/Scripts/jquery.MetaData.js",
                        "~/Scripts/jquery.validate.js",
                        "~/Scripts/bootstrap.js",
                        "~/Scripts/jquery.viewport.js",
                        "~/Scripts/jquery.cookie.js"
                     ));

如果构建在发布中,我希望发生这种情况。如果构建是在调试中,我希望加载未缩小的单个文件,以便调试很容易。

我能够做到这一点的唯一方法是在我的视图中写下以下内容:

    <%  if(HttpContext.Current.IsDebuggingEnabled)
        {
            Response.Write("<script type='text/javascript' src='../../Scripts/respond.min.js'></script>");
            Response.Write("<script type='text/javascript' src='../../Scripts/jquery.form.js'></script>");
            Response.Write("<script type='text/javascript' src='../../Scripts/jquery.MetaData.js'></script>");
            Response.Write("<script type='text/javascript' src='../../Scripts/jquery.validate.js'></script>");
            Response.Write("<script type='text/javascript' src='../../Scripts/bootstrap.js'></script>");
            Response.Write("<script type='text/javascript' src='../../Scripts/jquery.viewport.js'></script>");
            Response.Write("<script type='text/javascript' src='../../Scripts/jquery.cookie.js'></script>");
        }
        else
        {
            Scripts.Render("~/Scripts/Master-js");
        }
%> 

如你所见,我在这里重复一遍。还有更好的方法吗?

1 个答案:

答案 0 :(得分:7)

默认情况下,在视图上使用Scripts.Render()时,如果应用程序已启用调试(web.config中为debug="true"),则不会捆绑或缩小文件。此外,还将为该捆绑包中的每个资产呈现单个脚本标记。

您可以使用System.Web.Optimization.BundleTable.EnableOptimizations

手动启用/禁用捆绑和缩小功能