How does javascript version (asp-append-version) work in ASP.NET Core MVC?(javascript 版本(asp-append-version)如何在 ASP.NET Core MVC 中工作?)
问题描述
新的MVC似乎不支持动态捆绑(link),它应该使用 gulp 任务来完成.MVC 支持一些名为 asp-append-version
的新属性,但我没有找到任何关于它如何工作的解释.我怀疑它正在计算文件内容的一些哈希值,甚至在文件更改后更新它.有没有关于它如何工作的文档?
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 解析 razor 标记时重新计算哈希.
I am also wondering how it detects the file changes or whether it just recalculates the hash each time the MVC parses razor markup.
推荐答案
可以查看LinkTagHelper
源代码,你会看到它基本上是通过 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);
}
这个watcher由HostingEnvironment.WebRootFileProvider
提供,它实现了IFileProvider
:
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 中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:javascript 版本(asp-append-version)如何在 ASP.NET Core MVC 中工作?
基础教程推荐
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- SSE 浮点算术是否可重现? 2022-01-01