asp.net mvc自动压缩文件,并生成CDN引用
2021-06-30 18:06
标签:tip provided text dfa ble sap leo ansi adf ScriptsBundleTransform.cs BundleConfig.cs 这样的话就能生成 CDN 站点下的JS文件引用了 对了这里漏掉了一个地方, 但是如果CDN站点挂掉了怎么办,咱们继续: SctiptsBundleExtensions.cs 这样会生成如下的JS应用: 这样的话当CDN站点挂掉了还是能够访问到JS文件的。 接下来CSS怎么处理呢: StyleBundleExtensions.cs asp.net mvc自动压缩文件,并生成CDN引用 标签:tip provided text dfa ble sap leo ansi adf 原文地址:http://www.cnblogs.com/webenh/p/7135911.html很多站点都是用了静态文件分离。我推荐一种处理静态文件分离的方式。
BundleExtensions.cs public static class BundleExtensions { public static string Version = "1.0.0"; public static string ScriptsPath = "Cdn"; public static Bundle Production(this Bundle bundle, string cdn, string root, string minified, string full = "") { var transform = new ScriptsBundleTransform() { Version = Version, ScriptsPath = System.IO.Path.Combine("~/", ScriptsPath, root), Minified = minified, Full = full }; bundle.Transforms.Add(transform); bundle.CdnPath = cdn + "/" + root + "/" + string.Format("{0}?{1}", minified, Version); return bundle; } }
public class ScriptsBundleTransform : IBundleTransform { public string ScriptsPath { get; set; } public string Version { get; set; } public string Minified { get; set; } public string Full { get; set; } public ScriptsBundleTransform() { } public ScriptsBundleTransform(string path, string version, string minified, string full) { ScriptsPath = path; Version = version; Minified = minified; Full = full; } public void Process(BundleContext context, BundleResponse response) { string scriptsRoot = context.HttpContext.Server.MapPath(ScriptsPath); if (!Directory.Exists(scriptsRoot)) Directory.CreateDirectory(scriptsRoot); // if minified file name specified... if (!string.IsNullOrEmpty(Minified)) { using (TextWriter writer = File.CreateText(Path.Combine(scriptsRoot, Minified))) { writer.Write(response.Content); } } // if full file name specified... if (!string.IsNullOrEmpty(Full)) { using (Stream writer = File.OpenWrite(Path.Combine(scriptsRoot, Full))) { foreach (var file in response.Files) { file.VirtualFile.Open().CopyTo(writer); } } } } }
public static void RegisterBundles(BundleCollection bundles) { bundles.UseCdn = true; BundleTable.EnableOptimizations = true; //制定压缩后的资源存储路径 BundleExtensions.ScriptsPath = "Resource"; //制定请求时代的后缀,为了刷新掉客户端的缓存 BundleExtensions.Version = "00001"; //CDN站点地址 const string ajaxCdnPath = "http://localhost/Cdn"; bundles.Add(new ScriptBundle("~/bundles/bootstrap") .IncludeFallback("$.fn.affix", "~/Resource/Scripts/common/transition.js", "~/Resource/Scripts/common/button.js", "~/Resource/Scripts/common/affix.js", "~/Resource/Scripts/common/tab.js", "~/Resource/Scripts/common/collapse.js", "~/Resource/Scripts/common/tooltip.js", "~/Resource/Scripts/common/respond.js") .Production(ajaxCdnPath, "Scripts", "bootstrap.mincdn.js", "bootstrap.src.js")); bundles.Add(new ScriptBundle("~/Content/bootstrap") .Include("~/Resource/Content/bootstrap.css") .Include("~/Resource/Content/common/button.css") .Include("~/Resource/Content/common/bootstrap-theme.css") .Production(ajaxCdnPath, "Content", "bootstrap.mincdn.css", "bootstrap.src.css")); }
http://localhost/Cdn站点需要你在IIS中指定到你站点的Resource下,不然访问不到你的JS文件。
public static class SctiptsBundleExtensions { public static ScriptBundle IncludeFallback(this ScriptBundle bundle, string cdnFallbackExpression, string virtualPath, params IItemTransform[] transforms) { bundle.CdnFallbackExpression = cdnFallbackExpression; bundle.Include(virtualPath, transforms); return bundle; } public static ScriptBundle IncludeFallback(this ScriptBundle bundle, string cdnFallbackExpression, params string[] virtualPaths) { bundle.CdnFallbackExpression = cdnFallbackExpression; bundle.Include(virtualPaths); return bundle; } }
bundles.Add(new ScriptBundle("~/bundles/jquery") .IncludeFallback("window.jQuery", "~/Resource/Scripts/common/jquery-{version}.js") .Production(ajaxCdnPath, "Scripts", "jquery.mincdn.js", "jquery.src.js"));
public static class StyleBundleExtensions { ///
好吧后面的大家自己摸索吧:)上班了
很多代码是网上抄袭的,东拼西凑。
下一篇:web项目动态静态的区别
文章标题:asp.net mvc自动压缩文件,并生成CDN引用
文章链接:http://soscw.com/index.php/essay/99959.html