Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I dynamically add bundles after Application_Start has occurred?

I have an asp.net MVC4 web application that uses style bundling for themes. I have a physical themes folder structure like so...

 Themes\
     _Base\
     Theme1\
     Theme2\
     ...\

Each themes folder has an arbitrary number of LESS files in it. In my BundleConfig.RegisterBundles method, I have some logic that loops through each themes folder and creates a bundle for each. The bundling mechanism from System.Web.Optimization will watch for changes within the files and folders that are in existing bundles and flush the bundles cache, which works fine.

What I need, however, is a way for new theme folders (i.e. Theme3\) to be copied into my Themes root folder, and the application to recognize those without having to first restart it. I have tried creating a "dummy" bundle that references all files in every folder...

var changeTracking = new StyleBundle(BUNDLE_ROOT);
changeTracking.Transforms.Clear();
changeTracking.IncludeDirectory(THEME_ROOT, "*.less", true);
changeTracking.Transforms.Add(new LessTransform());
changeTracking.Transforms.Add(new CssMinify());
bundles.Add(changeTracking);

...but that doesn't seem to help. When I make Theme3\, it doesn't trigger another call to BundleConfig.RegisterBundles. I still have to do an IISRESET, recycle the application pool, etc. to get the new theme to be recognized.

Is there any way I can dynamically add bundles after Application_Start has occurred?

like image 471
racingcow Avatar asked Mar 10 '13 21:03

racingcow


1 Answers

This isn't something we explicitly are trying to support, the expectation is that all bundles are registered before the app starts. Otherwise this will cause issues in webfarm scenarios where some of bundles don't exist on all of your servers, which would result in 404s.

The bundle cache dependencies will take care of flushing old responses from the ASP.NET cache, but it would not trigger another call to RegisterBundles, that is called from your global.asax and would only be called during an app recycle like you've mentioned.

like image 95
Hao Kung Avatar answered Sep 20 '22 14:09

Hao Kung