【问题标题】:asp-append-version="true" not appending version to javascript fileasp-append-version="true" 不将版本附加到 javascript 文件
【发布时间】:2017-11-09 14:02:23
【问题描述】:

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

【问题讨论】:

标签: asp.net-core asp.net-core-2.0


【解决方案1】:

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

基本上你现在需要

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

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

【讨论】:

  • 这对你有用吗?我试过了,但它仍然没有将版本附加到脚本和 css 文件中。
  • @Priya 您使用的是哪个 .Net Core 版本?您是否看到上面的 Michael Freidgeim 评论,这适用于您吗?
  • 我的解决方案中有区域。可能就是这个原因。然后我在 Areas 文件夹中又添加了一个 _ViewImports.cshtml。它现在正在工作。谢谢:)
【解决方案2】:

在 MVC 中,我们可以通过维护配置值来进行版本控制,然后使用公共类将版本附加到 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;
    }
  }

【讨论】:

    【解决方案3】:

    在这种情况下,我提出了这个解决方案

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

    【讨论】:

    • 这将永远不会缓存客户端上的资源,因为每个页面视图都会附加一个新的随机字符串。
    猜你喜欢
    • 2016-04-25
    • 2023-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-11
    • 1970-01-01
    • 1970-01-01
    • 2018-01-18
    相关资源
    最近更新 更多