Versioning Content URLs in ASP.NET MVC 5
ASP.NET Core lets you add automatic versioning to JS and CSS files and I wanted to do the same in MVC 5.

Published on Wednesday, September 4, 2019

I just published a new Nuget package, AspNet.Mvc.AssetVersioning. This package extends the MVC5 @Url Razor helper with a new VersionedContent method which appends a Base64-encoded SHA256 hash to the end of the URL for cache-busting.

Versioning in ASP.NET Core

In ASP.NET Core, you can version URLs using the asp-append-version tag helper:

<script src="~/scripts/foo.js" type="text/javascript" asp-append-version="true"></script>

Which will output:

<script src="/scripts/foo.js?v=hash" type="text/javascript"></script>

Versioning in ASP.NET MVC 5

Since I have not yet migrated to ASP.NET Core for Keep Track of My Games, I needed the same functionality in order to remove some old libraries that are not available in .NET Core. Now you can achieve the same thing using my package:

@using System.Web.Mvc.AssetVersioning;

<script src="@Url.VersionedContent("~/scripts/foo.js")" type="text/javascript"></script>

This will output the following:

<script src="/scripts/foo?base64-encoded-hash" type="text/javascript"></script>

The helper will automatically cache hashes for files for the lifetime of the HttpContext. So, basically, for the lifetime of the application pool. Restart the site/app pool to refresh the cache, which should happen on any new deployments.

You can simplify the @using statement usage by adding the namespace to the /configuration/system.web.webPages.razor/pages/namespaces section in the Views/web.config file, as outlined in the GitHub repo README.

Hope this helps someone else facing the same issue!

Don't miss any updates!

No spam and I usually send a newsletter once a quarter with content you won't see on the blog.

About Kamran

I'm a technologist, speaker, and Pluralsight author and I specialize in building full-stack solutions with a focus on modern web technology and cloud native architecture.

Kamran's Pluralsight courses

comments powered by Disqus