@Blog.Author(Nandip Makwana) .LearningExperience(ASP.NET, ASP.NET MVC, IIS, jQuery & Technology Surrounding it...)

November 17, 2013 comment , , ,

[Updated] ASP.NET Web Optimization Framework & Cookieless Domain

In last week, one reader of this blog asked on twitter how to use Scripts.Render & Styles.Render with cookieless domain. Ahh wait... Still if you have not read my original post then please first read how to setup ASP.NET & MVC 4: Cookieless domain for bundling and static resources. Earlier in that post I have explained for both static & bundled resource but in that post I have used HTML Link & Script tag to serve bundled resource from static domain. Because Scripts.Render & Styles.Render generate Script & Link tag same as requested domain whereas in case of static domain Script & Link tag should not refer to requested domain but it should refer static domain. Here in this post we will see how to configure ASP.NET Web Optimization framework to use cookieless domain with Scripts.Render & Style.Render.

Why Scripts.Render & Styles.Render?

In general it really does not matter whether we use Script tag or Scripts.Render for bundled script. But yes Scripts.Render provide more control over ASP.NET Web Optimization framework. Two important of them is listed below.

  • Whether Debugging is Enabled or not Scripts.Render & Styles.Render check whether debugging is enable or not and accordingly it generate bundled url or individual Scripts or Link tag for each file in bundle.
  • Bundle Optimizations is Enabled or not Same way Scripts.Render & Styles.Render also check whether Optimizations is enabled or not and if it is enabled then it will generate bundled url and if it is disabled then it will generate separate Link & Script tag for each file in bundle.

Configure ASP.NET Web Optimization framework to use Cookieless Domain with Scripts.Render & Styles.Render

With earlier version of ASP.NET Web Optimization framework it was not possible to override default tag template for bundled scripts and styles so it was always referring requested domain while using Scripts.Render & Styles.Render. But in later version (1.1.2 when I'm writing this post) it is possible to override default tag template and hence we can override it to refer static domain with Scripts.Render & Styles.Render. So below is the steps to achieve the same.

  • Make sure that latest version (1.1.2 when I'm writing this) of ASP.NET Web Optimization framework installed. If earlier version is installed then first uninstall older version and then install latest version again.
  • Once latest version is installed open BundleConfig.cs and modify it to include overridden tag template as explained below.
public class BundleConfig
    public static void RegisterBundles(BundleCollection bundles)
        Styles.DefaultTagFormat = "<link href='http://static.domain.com{0}' rel='stylesheet'/>";
        Scripts.DefaultTagFormat = "<script src='http://static.domain.com{0}'></script>";

Yeah you are right. We have done! Here we are overriding default tag template so that whenever Scripts.Render & Styles.Render is used it will render static domain.

Allowing Bundled & Original Files to Serve from Static Domain.

Original article already explain how to allow Bundled & Static resource through static domain then also let me post more detailed code snippet. Below code snippet show bundle configuration.

public static void RegisterBundles(BundleCollection bundles)
    bundles.Add(new ScriptBundle("~/bundles/custom").Include(

And below updated code snippet show how to allow bundled url (i.e. ~/bundles/custom) and original files (i.e. ~/Scripts/script1.js, ~/Scripts/script2.js ) through Cookieless domain. For complete setup please first read original article here.

public class StaticResource : IHttpModule
    public void Init(HttpApplication context)
        context.BeginRequest += new EventHandler(context_BeginRequest);
    void context_BeginRequest(object sender, EventArgs e)
        HttpContext context = HttpContext.Current;
        string strUrl = context.Request.Url.OriginalString.ToLower();
        List<string> allowedUrls = new List<string>();
        allowedUrls.Add("/bundles/"); // FOR BUNDLED RESOURCE
        allowedUrls.Add("/scripts/"); // FOR ORIGINAL FILES
        if (allowedUrls.Any(u => strUrl.Contains(u)) == false)
            string strMainDomain = ConfigurationManager.AppSettings["MainDomain"];
    public void Dispose()


Hope this helpful! You can follow me on twitter for latest link and update on ASP.NET & MVC.

comments powered by Disqus

Featured Content

Resources & Tools

About Nandip Makwana

Nandip Makwana is passionate about digital world and web. He completed his Masters in Computer Application in June 2011. Currently he is working as a Software Engineer. He has shown great promise and command over ASP.NET and technologies surrounding it during his academic years and professorial life...continue reading