In ASP.NET, when EnableOptimization is set to false, the built-in bundling and minification feature is disabled. However, if you want to continue using a custom IBundleTransform even when EnableOptimization is off, you can achieve it by manually adding the script or stylesheet references to your HTML pages.

Here's how you can do it:

  1. Create a Custom BundleTransform: First, create your custom IBundleTransform implementation for bundling and minification. For example, let's create a custom transform to concatenate CSS files and add a comment at the top:

    public class CustomCssBundleTransform : IBundleTransform { public void Process(BundleContext context, BundleResponse response) { response.Content = "/* Custom CSS Bundle */" + Environment.NewLine + response.Content; } }
  2. Register the Custom BundleTransform: In your BundleConfig.cs or wherever you register your bundles, include the custom transform even when EnableOptimization is false. Here's an example:

    using System.Web.Optimization; public class BundleConfig { public static void RegisterBundles(BundleCollection bundles) { bundles.Add(new StyleBundle("~/bundles/customcss") .Include("~/Content/site.css") .Transforms.Add(new CustomCssBundleTransform())); } }
  3. Include the Bundle in HTML Pages: Since you have EnableOptimization set to false, the bundles will not be automatically rendered in your HTML pages. Therefore, you need to manually include the CSS or JavaScript files in your HTML layout or views:

    <!DOCTYPE html> <html> <head> <title>My Website</title> <!-- Other head elements --> <link href="@Url.Content("~/Content/site.css")" rel="stylesheet" /> </head> <body> <!-- Body content --> <script src="@Url.Content("~/Scripts/jquery.js")"></script> <!-- Other scripts --> </body> </html>

    Manually including the CSS and JavaScript files in your HTML pages ensures that your custom IBundleTransform will still be applied even when EnableOptimization is off.

  4. Disable Bundling in Web.config (Optional): You can also disable the built-in bundling feature in the web.config file to prevent any conflicts or unintended behavior:

    <appSettings> <!-- Other settings --> <add key="web:EnabledBundling" value="false" /> </appSettings>

By following these steps, your custom IBundleTransform will still be executed even when EnableOptimization is set to false. This approach allows you to have more control over the bundling and minification process, and you can apply custom transformations even in non-optimized scenarios.

Have questions or queries?
Get in Touch