Search code examples
asp.net-mvcbundleasp.net-optimization

ASP.NET MVC 4.0 Bundles with Multiple Absolute URLs for Styles


I have some of my application "Styles" and "Scripts" are referred from different environment applications.

  1. local-sites are referring from http://localhost:123/mystyles/default.css
  2. Dev site is referring from http://mydev.com/mystyles/default.css
  3. prod site is referring from http://prod.com/mystyles/default.css

I tried to tweak a little with cdn on bundles like below, but it didn't help.

public static void RegisterBundles(BundleCollection bundles)
        {
            bundles.Clear();
            bundles.ResetAll();

            BundleTable.EnableOptimizations = false;
            bundles.UseCdn = true;

            if (HttpRuntime.BinDirectory.Contains("local"))
                cdnHost = "http://localhost:123/";
            else if(condition)
                cdnHost = "http://mydev.com/";
            else
                cdnHost = "http://prod.com/";

            bundles.Add(new StyleBundle("~/Content/HRO/jquery/ui/css", cdnHost)
                .Include("~/css/jQuery/hro-0079c1/jquery-ui-1.8.11.custom.css"));

            bundles.Add(new StyleBundle("~/Content/HRO/jquery/ui/css", cdnHost)
               .Include("~/css/jQuery/hro-0079c1/jquery-ui-1.8.11.custom.css"));

            bundles.Add(new StyleBundle("~/Content/HRO/base/css", cdnHost)
               .Include("~/css/base.css"));

            bundles.Add(new StyleBundle("~/Content/HRO/reset/css", cdnHost)
               .Include("~/css/reset.css"));
        }

But this doesn't work. when I call these bundles as below

    @Styles.Render("~/Content/HRO/jquery/ui/css")
    @Styles.Render("~/Content/HRO/base/css")
    @Styles.Render("~/Content/HRO/reset/css")

I tried to debug the RegisterBundles(), looks like it is not creating those items with cdn url.

Is this a right way to create a absolute css URL for bundles? Am I missing anything here? Is there a better way to debug my RegisterBundles()?


Solution

  • You need to set both EnableOptimizations and EnableCDN to true for the cdn urls to be used.