How to force BundleCollection to flush cached script bundles in MVC4

前端 未结 6 1365
梦谈多话
梦谈多话 2020-11-29 19:28

... or how I learned to stop worrying and just write code against completely undocumented APIs from Microsoft. Is there any actual documentation of the official

6条回答
  •  囚心锁ツ
    2020-11-29 19:59

    I've got a similar problem.
    In my class BundleConfig I was trying to see what was the effect of using BundleTable.EnableOptimizations = true.

    public class BundleConfig
    {
        public static void RegisterBundles(BundleCollection bundles)
        {
            BundleTable.EnableOptimizations = true;
    
            bundles.Add(...);
        }
    }
    

    Everything was working fine.
    At some point I was doing some debugging and set the property to false.
    I struggled to understand what was happening cause it seemed that the bundle for jquery (the first one) wouldn't be resolved and loaded (/bundles/jquery?v=).

    After some swearing I think(?!) I've managed to sort things out. Try to add bundles.Clear() and bundles.ResetAll() at the beginning of the registration and things should start to work again.

    public class BundleConfig
    {
        public static void RegisterBundles(BundleCollection bundles)
        {
            bundles.Clear();
            bundles.ResetAll();
    
            BundleTable.EnableOptimizations = false;
    
            bundles.Add(...);
        }
    }
    

    I've realized I need to run these two methods only when I change the EnableOptimizations property.

    UPDATE:

    Digging deeper I've found out that BundleTable.Bundles.ResolveBundleUrl and @Scripts.Url seem to have problems to resolve the bundle path.

    For sake of simplicity I've added a few images:

    image 1

    I have turned off the optimization and bundled a few scripts.

    image 2

    The same bundle is included in the body.

    image 3

    @Scripts.Url gives me the "optimized" path of the bundle while @Scripts.Render generates the proper one.
    Same thing happens with BundleTable.Bundles.ResolveBundleUrl.

    I am using Visual Studio 2010 + MVC 4 + Framework .Net 4.0.

提交回复
热议问题