Versioning Content URLs in ASP.NET MVC 5
1 min read

Versioning Content URLs in ASP.NET MVC 5

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!

Enjoying these posts? Subscribe for more