javascript版本(asp-append-version)在ASP.NET Core MVC中如何工作? [英] How does javascript version (asp-append-version) work in ASP.NET Core MVC?

查看:311
本文介绍了javascript版本(asp-append-version)在ASP.NET Core MVC中如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

新MVC中似乎不支持动态捆绑(

It seems that there is no dynamic bundling supported in the new MVC (link), and it should be done using a gulp task. MVC supports some new attribute called asp-append-version, but I have not found any explanation on how it works. I suspect that it's calculating some hash of the file contents and even updates it after a file change. Is there any documentation on how it works?

我还想知道每次MVC解析剃刀标记时,它如何检测文件更改或是否只是重新计算哈希值.

I am also wondering how it detects the file changes or whether it just recalculates the hash each time the MVC parses razor markup.

推荐答案

您可以检查

You can check the LinkTagHelper source code, where you will see it is basically adding a version query string to the href value via a FileVersionProvider:

if (AppendVersion == true)
{
    EnsureFileVersionProvider();

    if (Href != null)
    {
        output.Attributes[HrefAttributeName].Value = _fileVersionProvider.AddFileVersionToPath(Href);
    }
}

private void EnsureFileVersionProvider()
{
    if (_fileVersionProvider == null)
    {
        _fileVersionProvider = new FileVersionProvider(
                HostingEnvironment.WebRootFileProvider,
                Cache,
                ViewContext.HttpContext.Request.PathBase);
    }
}

FileVersionProvider将使用SHA256算法计算文件内容的哈希值.然后,它将对其进行url编码,并将其添加到查询字符串中,如下所示:

The FileVersionProvider will calculate the hash of the file contents using the SHA256 algorithm. It will then url encode it and add it to the query string as in:

path/to/file?v=B95ZXzHiOuQJzhBoHlSlNyN1_cOjJnz2DFsr-3ZyyJs

仅当文件更改时,哈希才会重新计算,因为哈希已添加到缓存中,但具有基于文件查看器的过期触发器:

The hash will be recalculated only when the file changes, as it is added to the cache but with an expiration trigger based on a file watcher:

if (!_cache.TryGetValue(path, out value))
{
    value = QueryHelpers.AddQueryString(path, VersionKey, GetHashForFile(fileInfo));
    var cacheEntryOptions = new MemoryCacheEntryOptions().AddExpirationToken(_fileProvider.Watch(resolvedPath));
    _cache.Set(path, value, cacheEntryOptions);
}

此监视程序由实现IFileProviderHostingEnvironment.WebRootFileProvider提供:

This watcher is provided by HostingEnvironment.WebRootFileProvider, which implements IFileProvider:

//
// Summary:
//     Creates a change trigger with the specified filter.
//
// Parameters:
//   filter:
//     Filter string used to determine what files or folders to monitor. Example: **/*.cs,
//     *.*, subFolder/**/*.cshtml.
//
// Returns:
//     An Microsoft.Framework.Caching.IExpirationTrigger that is triggered when a file
//     matching filter is added, modified or deleted.
IExpirationTrigger Watch(string filter);

注意:您可以通过检查IMemoryCache中的值来自己查看缓存的值:

Note: You can see the cached values yourself by inspecting the values in the IMemoryCache:

//give the link:
<link rel="stylesheet" asp-append-version="true" href="~/css/site.css" />

//You can check the cached version
this.Context.RequestServices.GetRequiredService<IMemoryCache>().Get("/css/site.css")

//Which will show a value like:
/css/site.css?v=B95ZXzHiOuQJzhBoHlSlNyN1_cOjJnz2DFsr-3ZyyJs

这篇关于javascript版本(asp-append-version)在ASP.NET Core MVC中如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆