asp-append-version="true" this should append version to script. With .net core 1.1 it worked just fine. Recently upgraded to version 2.0 and it no longer works. Any ideas why?
3 Answers
https://github.com/MarkPieszak/aspnetcore-angular2-universal/issues/471
Basically you now need
@addTagHelper "*, Microsoft.AspNetCore.Mvc.TagHelpers"
And to make it easier, you can have a global _ViewImports.cshtml file under the Views folder, and just throw that line in there and it will apply the line to all View pages.
-
did this work for you? I tried but its still not appending version to scripts & css files.– PriyaCommented Sep 22, 2020 at 6:58
-
@Priya Which .Net Core version are you using? Did you see Michael Freidgeim comment above, does that apply to you?– goamnCommented Sep 23, 2020 at 4:05
-
2I have Areas in my solution. May be that was the reason. I then added one more _ViewImports.cshtml inside Areas folder. And its working now. Thanks :)– PriyaCommented Sep 25, 2020 at 10:40
In MVC, we can do versioning by maintaining config value then used public class to append the version in JS and CSS reference.
CS:
public static class StaticFileHelper
{
static string staticVersion;
static StaticFileHelper()
{
staticVersion = System.Configuration.ConfigurationManager.AppSettings["JSVersioning"];
}
public static string StaticFile(this UrlHelper html, string filename)
{
var virtualPath = ReleaseVirtualPath(filename);
var root = html.RequestContext.HttpContext.Request.ApplicationPath;
if (root.Length > 1)
{
virtualPath = root + virtualPath;
}
return virtualPath;
}
}
In case of this I made this solution
<link rel="stylesheet" type="text/css" href="\css\[email protected](9)">
So my random generator is generating different strings for version then I never face with this situation again. Also I am sharing random string generator for interested people.
private static Random random = new Random();
public static string RandomStringGenerator(int length)
{
const string chars = "abcdefghijklmnopqrstuwxyz0123456789";
return new string(Enumerable.Repeat(chars, length)
.Select(s => s[random.Next(s.Length)]).ToArray());
}
-
This will never cache the resource on clients as each page view gets a new random string attached. Commented Jun 23, 2021 at 14:42