📜 ⬆️ ⬇️

We're friends of System.Web.Optimization Bundle with Windows Azure Storage

I waited, waited, waited, when smart guys developing System.Web.Optimization would add support for automatically downloading the contents of the bundle to the Azure Storage container. “1.0.0-beta2” was released the other day, but I didn’t find the desired functionality, but I want speed ...



How to use?


<head> @BlobStorageProvider.Render("~/mainlayout", BlobStorageProvider.ContentType.Style) </head> 

How does it work?


 /// <summary> ///   - Blob' /// </summary> private const string CachedStorageLink = "http://cdn.m6lab.com/{0}/{1}"; 
The above code contains a link to the associated Caching for Blob. The response speed of Blob leaves much to be desired, so I recommend using Caching to get data from Blob.
')
It is not possible to override the function that creates the Bundle, so I had to use a crutch:
 //     var content = new WebClient().DownloadString("{URL  }" + url); 


Source code entirely:
 public class BlobStorageProvider { /// <summary> ///      /// </summary> public enum ContentType { Script, Style } /// <summary> ///   - Blob' /// </summary> private const string CachedStorageLink = "http://cdn.m6lab.com/{0}/{1}"; /// <summary> ///  Blob- /// </summary> private const string ContainerName = "robofollower"; /// <summary> ///    Blob' /// </summary> private static readonly CloudStorageAccount StorageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName={Storage Account};AccountKey={Primary Key}"); /// <summary> ///  Blob'a /// </summary> private static readonly CloudBlobContainer Container = StorageAccount.CreateCloudBlobClient().GetContainerReference(ContainerName); /// <summary> ///    CDN /// </summary> private static readonly Dictionary<string, string> CdnLinks = new Dictionary<string, string>(); /// <summary> ///    Blob- /// </summary> /// <param name="virtualPath"></param> /// <param name="contentType"></param> /// <returns></returns> private static void Upload(string virtualPath, ContentType contentType) { //      var url = Scripts.Url(virtualPath); //     var content = new WebClient().DownloadString("{URL  }" + url); //    var formatted = url.ToString().Replace("/", "").Replace("?", "").Replace("v", "").Replace("=", "").Replace("-", ""); var blobContentType = string.Empty; //      switch (contentType) { case ContentType.Script: formatted += ".js"; blobContentType = "text/javascript"; break; case ContentType.Style: formatted += ".css"; blobContentType = "text/css"; break; } //      var blob = Container.GetBlockBlobReference(formatted); blob.Properties.CacheControl = "public"; blob.Properties.ContentType = blobContentType; //    blob.UploadFromStream(new MemoryStream(Encoding.ASCII.GetBytes(content))); //      CdnLinks.Add(virtualPath, string.Format(CachedStorageLink, ContainerName, formatted)); } public static IHtmlString Render(string virtualPath, ContentType contentType) { if (!CdnLinks.ContainsKey(virtualPath)) Upload(virtualPath, contentType); switch (contentType) { case ContentType.Style: { var tag = new TagBuilder("link"); tag.MergeAttributes(new Dictionary<string, string> { { "href", CdnLinks[virtualPath] }, { "rel", "stylesheet" } }); return new HtmlString(tag.ToString(TagRenderMode.SelfClosing)); } case ContentType.Script: { var tag = new TagBuilder("script"); tag.MergeAttributes(new Dictionary<string, string> { { "src", CdnLinks[virtualPath] } }); return new HtmlString(tag.ToString(TagRenderMode.SelfClosing)); } } return null; } } 

Source: https://habr.com/ru/post/157597/


All Articles