liveBook · Manning

Append version tag helper

As explained in Pro ASP.NET Core 7, Tenth Edition

The asp-append-version attribute is a Razor tag helper attribute that enables version-value generation for an image resource URL.

Cached static files can outlive an application update, leaving a page paired with stale images or JavaScript. Set asp-append-version="true" on a local resource element to enable cache busting: its generated URL gains a checksum-based query string. section 26.4.1 section 26.5

Listing 26.16 Adding an image in the Views/Shared/_SimpleLayout.cshtml file
<!DOCTYPE html>
<html>
<head>
    <title>@ViewBag.Title</title>
    <link href="/lib/bootstrap/css/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
    <div class="m-2">
        <img src="/images/city.png" asp-append-version="true" 
            class="m-2" />
        @RenderBody()
    </div>
</body>
</html>

A shared Razor layout puts the attribute directly on an image: <img src="/images/city.png" asp-append-version="true" class="m-2" />. section 26.5

Check the generated image URL

Request http://localhost:5000/home/list and inspect the HTML response. The image URL appears as /images/city.png?v=KaMNDSZFAJufRcRDpKh0K_IIPNc7E. The checksum changes when the file changes, so a cache sees a new URL rather than serving its old copy. section 26.5 section 26.4.1

The same attribute works on script elements through ScriptTagHelper. It can accompany asp-src-include and asp-src-exclude; a selected local jQuery file is emitted with a ?v= checksum. File selection chooses what to load, while the versioned URL addresses stale cached content. section 26.4.1

How Pro ASP.NET Core 7, Tenth Edition covers it

How deep it goes
Shows attribute placement, generated checksums, and cache behavior; does not detail checksum calculation.
Examples
Uses a shared-layout city image and a selected local jQuery script, each with a generated versioned URL.
What it assumes
Assumes basic HTML element and Razor layout familiarity; demonstrates attribute placement rather than teaching those foundations.
What you can do after
Apply it via ImageTagHelper or ScriptTagHelper and inspect generated HTML to verify the versioned URL.
How it connects
Connects Razor tag helpers, static-file caching, cache busting, ImageTagHelper, and ScriptTagHelper.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pro ASP.NET Core 7, Tenth Edition. The section links point to the parts of the book it draws on.