Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does javascript version (asp-append-version) work in ASP.NET Core 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?

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

like image 882
Ilya Chernomordik Avatar asked Oct 26 '15 09:10

Ilya Chernomordik


People also ask

What is ASP append version?

asp-append-versionIf the web server is unable to obtain read access to the static file, no v parameter is added to the src attribute in the rendered markup. For a Tag Helper to generate a version for a static file outside wwwroot , see Serve files from multiple locations.

How can add JavaScript in ASP.NET MVC?

The recommended approach is to put in a separate JavaScript file or inside a section defined in Layout page. A section can be added in the MVC Layout page using @RenderSection() directive. For example, we can define a section in Layout page under <head> tag for scripts like below.

Is JavaScript used in ASP.NET MVC?

JavaScript can be used in asp.net mvc. If you go for Asp.NET Web forms, there is no need to have a detailed understanding of HTML, CSS or JavaScript as it abstracts all these details and provides automatic plumbing.

What is _ViewImports Cshtml?

The _ViewImports. cshtml file for an ASP.NET Core MVC app is typically placed in the Pages (or Views) folder. A _ViewImports. cshtml file can be placed within any folder, in which case it will only be applied to pages or views within that folder and its subfolders.


1 Answers

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

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

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

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 
like image 166
Daniel J.G. Avatar answered Sep 28 '22 08:09

Daniel J.G.