使い方は?
<head> @BlobStorageProvider.Render("~/mainlayout", BlobStorageProvider.ContentType.Style) </head>
どのように機能しますか?
/// <summary> /// - Blob' /// </summary> private const string CachedStorageLink = "http://cdn.m6lab.com/{0}/{1}";
上記のコードには、添付されたBlobへのキャッシュへのリンクがあります。 Blobの応答速度が遅いため、キャッシュを使用してBlobからデータを取得することをお勧めします。
バンドルを作成する関数をオーバーライドすることはできないため、松葉杖を使用する必要がありました。
// var content = new WebClient().DownloadString("{URL }" + url);
全体としてのソースコード:
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; } }