asp-append-version="true" not appending version to javascript file

Viewed 7582

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

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\custom.css?v=@Generator.RandomStringGenerator(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());
    }
Related