Asp.net-mvc
In Asp.net-mvc you can try the solution of Soen’s reply
public static class JavascriptExtension
{
public static MvcHtmlString IncludeVersionedJs(this HtmlHelper helper, string filename)
{
string version = GetVersion(helper, filename);
return MvcHtmlString.Create("<script type='text/javascript' src='" + filename + version + "'></script>");
}
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 System.IO.FileInfo(physicalPath).LastWriteTime.ToString("MMddHHmmss")}";
context.Cache.Add(filename, version, null,
DateTime.Now.AddMinutes(5), TimeSpan.Zero,
CacheItemPriority.Normal, null);
return version;
}
else
{
return context.Cache[filename] as string;
}
}
}
And in View call it that:
@Html.IncludeVersionedJs("/meuscriptminificado.js")
That will result in this:
<script type='text/javascript' src='/meuscriptminificado.js?20111129120000'></script>
ASP.NET Core MVC
In Asp.net core (Asp.net-mvc 6) there is the asp-append-version
and you can use it like this:
<script src="scripts/meujs.js" asp-append-version="true"></script>
<link href="styles/meucss.css" rel="stylesheet" asp-append-version="true" />
PHP
With PHP you can use filemtime
, something that would be like:
<link rel="stylesheet" href="styles/meucss.css?v=<?php echo filemtime('caminho/real/styles/meucss.css'); ?>">
<script src="scripts/meujs.js?v=<?php echo filemtime('caminho/real/scripts/meujs.js'); ?>"></script>
However create a function that can facilitate a lot and in case just call IncludeVersioned
, thus:
<?php
function IncludeVersioned($file) {
$absoluto = 'ect/var/www/caminho/dos/resources/'; # ajuste aqui
if (is_file($absoluto . $file)) {
$time = $absoluto . $file;
$ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
switch ($ext) {
case 'js':
echo '<script src="', $file,'?v=', $time,'"></script>';
break;
case 'css':
echo '<link href="', $file,'?v=', $time,'" rel="stylesheet">;
break;
default:
echo '<!-- tipo invalido de resource -->';
}
} else {
echo '<!-- resource não encontrado -->';
}
}
?>
<?php IncludeVersioned('scripts/foo.js'); ?>
<?php IncludeVersioned('styles/foo.css'); ?>
deleted the browser cache before trying?
– ihavenokia
PHP or Asp.net or something else?
– Guilherme Nascimento
@Guilhermenascimentop. want to do in Asp.net same
– T. Morciani
@ihavenokia yes, but I would like to implement a solution of this kind
– T. Morciani