0

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()?

tshepang
  • 12,111
  • 21
  • 91
  • 136
HaBo
  • 13,999
  • 36
  • 114
  • 206

4 Answers4

1

If it is just a matter of getting absolute url in bundle then you can go for this.

public static class Extensions
    {
        public static IHtmlString RenderScript(this UrlHelper helper, params string[] paths)
        {
            string scripts = System.Web.Optimization.Scripts.Render(paths).ToHtmlString();
            string hostName = HttpContext.Current.Request.Url.Scheme + Uri.SchemeDelimiter + HttpContext.Current.Request.Url.Authority;
            string replaced = Regex.Replace(scripts, "src=\"/", "src=\"" + hostName + "/", RegexOptions.Multiline | RegexOptions.IgnoreCase);
            return new HtmlString(replaced);
        }
    }

This will basically take the bahvior from Scripts.Render and then apply absolute urls to it. Then in the view you have to write

  @Url.RenderScript("~/bundles/jquery")

instead of

  @Scripts.Render("~/bundles/jquery")

Enjoy coding!!...

Dhrumil Bhankhar
  • 1,301
  • 1
  • 14
  • 15
1

Scripts and Styles from System.Web.Optimization Supports RenderFormatmethod that can be used in the following way:

@{
    var hostName = "www.example.com";
    var styleFormat = "<link href=\"" + hostName+ "{0}\" type=\"text/css\" rel=\"stylesheet\"/>";
}


@Styles.RenderFormat(styleFormat, "~/styles/vendor")
BotanMan
  • 1,357
  • 12
  • 25
0

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

Hao Kung
  • 28,040
  • 6
  • 84
  • 93
0

Try for example, some like this:

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

well I could do. What I could do is to group more than one style in one bundle.

good luck!