13

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?

1

3 Answers 3

24

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.

3
  • did this work for you? I tried but its still not appending version to scripts & css files.
    – Priya
    Commented 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?
    – goamn
    Commented Sep 23, 2020 at 4:05
  • 2
    I 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 :)
    – Priya
    Commented Sep 25, 2020 at 10:40
1

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;
    }
  }
-4

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());
    }
1
  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.