admin管理员组

文章数量:1418700

I am using the ASP.NET Bundling mechanism:

            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"
                     ));

I want this to happen if the build is in release. If the build is in debug, I want the un-minified individual files to load so debugging would be easy.

The only way I have been able to do this is to write the following in my view:

    <%  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");
        }
%> 

As you can see, I am repeating myself here. Is there a better way?

I am using the ASP.NET Bundling mechanism:

            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"
                     ));

I want this to happen if the build is in release. If the build is in debug, I want the un-minified individual files to load so debugging would be easy.

The only way I have been able to do this is to write the following in my view:

    <%  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");
        }
%> 

As you can see, I am repeating myself here. Is there a better way?

Share Improve this question asked Nov 4, 2013 at 18:41 BarkaBarka 8,94216 gold badges69 silver badges94 bronze badges 2
  • 4 If you run the site in debug mode and use Scripts.Render("~/Scripts/Master-js"); on your view, it should have the non-minified version served with separate script tags for each. – Steven V Commented Nov 4, 2013 at 18:44
  • @StevenV, if you add your ment as a anser, I will mark it as correct. Many thanks for your help. – Barka Commented Nov 11, 2013 at 23:48
Add a ment  | 

1 Answer 1

Reset to default 7

By default, when using Scripts.Render() on your view and if the application has debugging enabled (debug="true" in the web.config) the files will not be bundled or minified. Also a individual script tag will be rendered for every asset in that bundle.

You can manually enable/disable bundling and minification by using System.Web.Optimization.BundleTable.EnableOptimizations

本文标签: cBundling and not bundling in production and test environments in ASPNET MVCStack Overflow