背景:公司一个老项目,前端引用都是直接引用文件,这样导致每次发布都存在用户浏览器缓存问题,常见做法是找到每个引用前端文件处加?version,但是太麻烦了
解决思路:
1.找到所有前端文件
2.jenkins每次编译更新版本号,把新的版本号拼接到前端文件引用处
实现:
-
1.首先在扩展类里面扩展以下方法,用来动态处理版本变更的拼接
/// <summary>
/// 处理js
/// </summary>
/// <param name="html"></param>
/// <param name="contentPath"></param>
/// <returns></returns>
public static MvcHtmlString Script(this HtmlHelper html, string contentPath)
{
return VersionContent(html, "<script src=\"{0}\" type=\"text/javascript\"></script>", contentPath);
}
/// <summary>
/// 处理css
/// </summary>
/// <param name="html"></param>
/// <param name="contentPath"></param>
/// <returns></returns>
public static MvcHtmlString Style(this HtmlHelper html, string contentPath)
{
return VersionContent(html, "<link href=\"{0}\" rel=\"stylesheet\" type=\"text/css\">", contentPath);
}
/// <summary>
/// 加上版本
/// </summary>
/// <param name="html"></param>
/// <param name="template"></param>
/// <param name="contentPath"></param>
private static MvcHtmlString VersionContent(this HtmlHelper html, string template, string contentPath)
{
string v = "";
try
{
var versionPath = ConfigHelper.GetConfigString("VersionPath");
var version = FileUtility.GetContent(versionPath);
var httpContenxt = html.ViewContext.HttpContext;
contentPath = UrlHelper.GenerateContentUrl(contentPath, httpContenxt) + "?v=" + version;
v = string.Format(template, contentPath);
}
catch (Exception ex)
{
ExceptionlessManager.WriteExceptionLog("HtmlHelperExtension", $"初始化版本号异常:{ex}", null, AllProjectsKey.ProductError.ToString());
}
return MvcHtmlString.Create(v);
}
-
2.一次性替换所有静态文件的引用方式
引用Less组件,参考文档:Less.Html
static void Main(string[] args)
{
//静态文件包地址,比如MVC项目中Views文件夹
var InitStaticResourcesPath = ConfigurationManager.AppSettings["InitStaticResourcesPath"];
if (!string.IsNullOrEmpty(InitStaticResourcesPath))
{
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(InitStaticResourcesPath);
var files = dir.GetFiles(".", System.IO.SearchOption.AllDirectories);
var aspxs = files.ToList().Where(w => w.FullName.Contains("aspx"));
var cshtmls = files.ToList().Where(w => w.FullName.Contains("cshtml"));
foreach (var aspx in aspxs)
{
var html = FileUtility.GetContent(aspx.FullName);
Document document = HtmlParser.Parse(html);
var q = Selector.Bind(document);
q("html").before("<%@ Import Namespace=\"PDSys.Domain.Models.Extentions\" %>");
q("html").before("\r\n");
var heads = q("head");
var links = q("link");
var scripts = q("script");
foreach (var item in links)
{
var href = item.attributes.Where(w => w.name == "href").FirstOrDefault();
if (href != null)
{
var start = href.ToString().IndexOf("\"");
var end = href.ToString().IndexOf(".css");
if (end > 0)
{
var content = href.ToString().Substring(start, end);
q(item).before($"<%=Html.Style({content}) %>");
q(item).remove();
}
}
}
foreach (var item in scripts)
{
var src = item.attributes.Where(w => w.name == "src").FirstOrDefault();
if (src != null)
{
var start = src.ToString().IndexOf("\"");
var end = src.ToString().IndexOf(".js");
if (end > 0)
{
var content = src.ToString().Substring(start, end);
q(item).before($"<%=Html.Script({content}) %>");
q(item).remove();
}
}
}
html = document.ToString();
FileUtility.SetContent(aspx.FullName, html);
Console.WriteLine($"{aspx.FullName}处理完成");
}
foreach (var cshtml in cshtmls)
{
var html = FileUtility.GetContent(cshtml.FullName);
Document document = HtmlParser.Parse(html);
var q = Selector.Bind(document);
q("html").before("@using PDSys.Domain.Models.Extentions");
q("html").before("\r\n");
var heads = q("head");
var links = q("link");
var scripts = q("script");
foreach (var item in links)
{
var href = item.attributes.Where(w => w.name == "href").FirstOrDefault();
if (href != null)
{
var start = href.ToString().IndexOf("\"");
var end = href.ToString().IndexOf(".css");
if (end > 0)
{
var content = href.ToString().Substring(start, end);
q(item).before($"@Html.Style({content})");
q(item).remove();
}
}
}
foreach (var item in scripts)
{
var src = item.attributes.Where(w => w.name == "src").FirstOrDefault();
if (src != null)
{
var start = src.ToString().IndexOf("\"");
var end = src.ToString().IndexOf(".js");
if (end > 0)
{
var content = src.ToString().Substring(start, end);
q(item).before($"@Html.Script({content})");
q(item).remove();
}
}
}
html = document.ToString();
FileUtility.SetContent(cshtml.FullName, html);
Console.WriteLine($"{cshtml.FullName}处理完成");
}
Console.WriteLine("InitStaticResourcesPath全部处理完成");
Console.ReadKey();
return;
}
}
标签:src,Asp,item,href,Framework,html,ToString,var,Net
From: https://www.cnblogs.com/BOSET/p/17276177.html