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("", fileName, version, async);
return MvcHtmlString.Create(result);
}
public static MvcHtmlString RenderStyle(this HtmlHelper helper, string filename)
{
var version = GetVersion(helper, filename);
return MvcHtmlString.Create("");
}
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;
}
}
}
}