Bundling not working in MVC5 when I turn on release mode

后端 未结 7 1643
耶瑟儿~
耶瑟儿~ 2020-12-15 03:46

I have the following bundle configured in BundleConfig.cs:

bundles.Add(new StyleBundle(\"~/bundles/css\").Include(
                      \"~/assets/bootstrap         


        
相关标签:
7条回答
  • 2020-12-15 03:59

    The default Release Web.config transform removes the debug attribute like so:

    <compilation xdt:Transform="RemoveAttributes(debug)" />
    

    However, this will not cause the expected bundling behavior to occur. Instead, you must create a transform that literally sets the debug attribute to "false", like so:

    <compilation debug="false" xdt:Transform="SetAttributes" />
    
    0 讨论(0)
  • I had this problem when I changed the css folder. My problem was that I changed the BundleConfig file for css files:

    BundleConfig.cs:

    bundles.Add(new StyleBundle("~/Content/css").Include(
                          "~/Content/css/bootstrap.css",
       ....
    

    Views included:

    @Styles.Render("~/Content/css")
    

    For some reason, this didn't worked. 'bundles' seems to be a keyword or something (not sure). You must left it this way:

    bundles.Add(new StyleBundle("~/bundles/css").Include(
                          "~/Content/css/bootstrap.css", ...
    

    Views:

    ...
    @Styles.Render("~/bundles/css")
    ...
    

    I have no need to modify 'BundleTable.EnableOptimizations' nor web.config nor anything else.

    Hope this helps someone.

    0 讨论(0)
  • 2020-12-15 04:13

    This is the default behavior.

    Bundling and minification is enabled or disabled by setting the value of the debug attribute in the compilation Element in the Web.config file.

    http://www.asp.net/mvc/overview/performance/bundling-and-minification

    enter image description here

    0 讨论(0)
  • 2020-12-15 04:14
    <compilation debug="true" targetFramework="4.5.2" />
    

    solve my problem. debug=true was missing in my web config while publishing.

    0 讨论(0)
  • 2020-12-15 04:19

    Another possible issue is that inside of your Global.asax.cs file in the Application_Start() method, you're missing the call to your BundleConfig.

    For example, assuming your method is using the default name RegisterBundles(BundleCollection bundles) then inside of your Global.asax.cs file, you'll want to add BundleConfig.RegisterBundles(BundleTable.Bundles); in the Application_Start() method.

    So your Global.asax.cs file might look something like this:

    using System;
    using System.Collection.Generic;
    using System.Linq;
    using System.Web;
    using System.Http;
    using System.Mvc;
    using System.Routing;
    using System.Optimization;
    
    namespace MyProject
    {
        public class MvcApplication : System.Web.HttpApplication
        {
            AreaRegistration.RegisterAllAreas();
    
            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
    
            // ** This line right below might be what you are missing **
            BundleConfig.RegisterBundles(BundleTable.Bundles);
    
        }
    }
    
    0 讨论(0)
  • 2020-12-15 04:21

    The way that I get around this is to force it in the BundleConfig to do exactly what I want it to do. I don't think MVC4 had the same options with the config file (or I just never got them to work).

    So this is what I have at the end of my RegisterBundles method:

    #if DEBUG
    BundleTable.EnableOptimizations = false;
    #else
    BundleTable.EnableOptimizations = true;
    #endif
    

    This way it's always there, plain to see. However, you do have to remember to put that in there when you're starting up the project, but that's not a huge deal.

    If you're not familiar with these, the #if DEBUG is a preprocessor directives that tells the CLR to do what is in that block when the DEBUG build parameter is present (should only be present in DEBUG mode, though that can be changed from the Project Properties). If that variable is not present (Release mode, or any other mode), then it will do the other block.

    0 讨论(0)
提交回复
热议问题