I'm trying to reload reviewed css/js files automatically to prevent cache. I can loop trough header (which is runat="server") and add a value to link href references but the loop doesn't catch script tags as controls.
if (Page.Header != null)
{
foreach (var sc in Page.Header.Controls)
{
var type = sc.GetType();
if (type == typeof(HtmlLink))
{
((HtmlLink)sc).Href += "?v=" + Tools.BuildDate();
}
else if (type == typeof(HtmlGenericControl))
{
((HtmlGenericControl)sc).Attributes["src"] += "?v=" + Tools.BuildDate();
}
}
}
I've tried to add runat="server" to script tags but in this way asp.net tries to compile those files and build fails. I cannot add version every time because the project has many files. Also code blocks is not allowed when runat="server" is applied.
I want to use a commonly accepted solution so I don't have to worry for css/js file versions any more.
In MVC Core 1.0 there is a tag helper for this exact purpose. If you are using a different version of MVC then the previous answers should help.
<link rel="stylesheet" src="wherever.css" asp-append-version="true" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With