40 lines
1.4 KiB
C#
40 lines
1.4 KiB
C#
namespace System.Web.Mvc.Html
|
|
{
|
|
public static class CacheExtension
|
|
{
|
|
public static MvcHtmlString RenderScript(this HtmlHelper helper, string filename, bool asyncLoad = false)
|
|
{
|
|
var fileName = VirtualPathUtility.ToAbsolute(filename);
|
|
var version = GetVersion(helper, filename);
|
|
var async = asyncLoad ? " async='async'" : "";
|
|
var result = String.Format("<script src='{0}{1}' type='text/javascript'{2}></script>", fileName, version, async);
|
|
return MvcHtmlString.Create(result);
|
|
}
|
|
|
|
public static MvcHtmlString RenderStyle(this HtmlHelper helper, string filename)
|
|
{
|
|
var version = GetVersion(helper, filename);
|
|
return MvcHtmlString.Create("<link href='" + VirtualPathUtility.ToAbsolute(filename) + version + "' rel='stylesheet' type='text/css'>");
|
|
}
|
|
|
|
private static string GetVersion(this HtmlHelper helper, string filename)
|
|
{
|
|
var context = helper.ViewContext.RequestContext.HttpContext;
|
|
|
|
if (context.Cache[filename] == null)
|
|
{
|
|
var physicalPath = context.Server.MapPath(filename);
|
|
var version = "?v=" + new IO.FileInfo(physicalPath).LastWriteTime.ToString("yyyyMMddHHmmss");
|
|
|
|
context.Cache[filename] = version;
|
|
|
|
return version;
|
|
}
|
|
else
|
|
{
|
|
return context.Cache[filename] as string;
|
|
}
|
|
}
|
|
}
|
|
}
|