Search code examples
c#asp.net.netasp.net-optimizationsystem.web.optimization

Is there any way to read System.Web.Optimization contents from code-behind?


Is there any way to read System.Web.Optimization contents from code-behind? I tried poking around the dll, but I couldn't find a way to read the bundle contents (as far as I can tell, it was meant to work via a handler, which handles the calls to the bundles.

My intent is to use sass, which would cause heavier processing because of the transformations (on top of the minification), and generate it as a file, which will then be served to the end-user. I found one solution by reading the file with a WebRequest and saving the response, but it just feels to hackish to me.


Solution

  • You can do this via the Optimizer classes, this code is current as of the 1.1-beta1 release:

            BundleCollection bundles = new BundleCollection();
            bundles.Add(new StyleBundle("~/bundles/css").Include("~/Styles/image.css", "~/Styles/nested/image2.css"));
            OptimizationSettings config = new OptimizationSettings() {
                ApplicationPath = TestContext.DeploymentDirectory,
                BundleTable = bundles
            };
    
            BundleResponse response = Optimizer.BuildBundle("~/bundles/css", config);
    

    And response.Content is the raw bundle data.