title: How to upload bundled and minified files to Windows Azure CDN
body: <p>I'm using the ASP.NET MVC 4 bundling and minifying features in the <a href="https://www.nuget.org/packages/Microsoft.AspNet.Web.Optimization" rel="noreferrer"><code>Microsoft.AspNet.Web.Optimization</code></a> namespace (e.g. <code>@Styles.Render("~/content/static/css")</code>).</p>
  
  <p>I'd like to use it in combination with a Windows Azure CDN.</p>
  
  <p>I looked into writing a custom <code>BundleTransform</code> but the content is not optimized there yet.</p>
  
  <p>I also looked into parsing and uploading the optimized stream on runtime but that feels like a hack to me and I don't really like it:</p>
  
  <pre><code>@StylesCdn.Render(Url.AbsoluteContent(
      Styles.Url("~/content/static/css").ToString()
      ));
  
  public static IHtmlString Render(string absolutePath)
  {
      // get the version hash
      string versionHash = HttpUtility.ParseQueryString(
          new Uri(absolutePath).Query
          ).Get("v");
  
      // only parse and upload to CDN if version hash is different
      if (versionHash != _versionHash)
      {
          _versionHash = versionHash;
  
          WebClient client = new WebClient();
          Stream stream = client.OpenRead(absolutePath);
  
          UploadStreamToAzureCdn(stream);
      }
  
      var styleSheetLink = String.Format(
          "&lt;link href=\"{0}://{1}/{2}/{3}?v={4}\" rel=\"stylesheet\" type=\"text/css\" /&gt;",
          cdnEndpointProtocol, cdnEndpointUrl, cdnContainer, cdnCssFileName, versionHash
          );
  
      return new HtmlString(styleSheetLink);
  }
  </code></pre>
  
  <p>How can I upload the bundled and minified versions automatically to my Windows Azure CDN?</p>