asp-append-version="true" 不将版本附加到 javascript 文件

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

asp-append-version="true" 这应该将版本附加到脚本。使用 .net core 1.1 它工作得很好。最近升级到 2.0 版,它不再工作。有什么想法吗?

https://github.com/MarkPieszak/aspnetcore-angular2-universal/issues/471

基本上你现在需要

@addTagHelper "*, Microsoft.AspNetCore.Mvc.TagHelpers"

为了方便起见,您可以在 Views 文件夹下创建一个全局 _ViewImports.cshtml 文件,只需将那一行放入其中,它就会将该行应用于所有视图页面。

在这种情况下我做了这个解决方案

<link rel="stylesheet" type="text/css" href="\css\custom.css?v=@Generator.RandomStringGenerator(9)">

所以我的随机生成器为版本生成了不同的字符串,然后我再也没有遇到过这种情况。另外,我正在为感兴趣的人分享随机字符串生成器。

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

在 MVC 中,我们可以通过维护配置值来进行版本控制,然后使用 public class 在 JS 和 CSS 引用中附加版本。

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