Cache Busting in .NET

<script src=”~/jquery.js?dt=@(DateTime.Now.Ticks)”></script>
Adding a timestamp to file
Timestamp change in every request
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Caching;using System.Web.Hosting;using System.IO;namespace CacheBurstingMVC{public class CacheUrlExtension{public static string Extend(string relativePath){if (HttpRuntime.Cache[relativePath] == null){string absolutePath = HostingEnvironment.MapPath(“~” + relativePath);DateTime date = File.GetLastWriteTime(absolutePath);int index = relativePath.LastIndexOf(‘/’);string result = relativePath.Insert(index, “/v-” + date.Ticks);HttpRuntime.Cache.Insert(relativePath, result, new CacheDependency(absolutePath));}return HttpRuntime.Cache[relativePath] as string;}}}
@{Layout = null;}<!DOCTYPE html><html><head><meta name=”viewport” content=”width=device-width” /><title></title><link rel=”stylesheet” href=@CacheBurstingMVC.CacheUrlExtension.Extend(“/Content/styles.css”)/></head><body><div>Home</div></body></html>
<system.webServer><rewrite><rules><rule name=”fingerprint”><match url=”([\S]+)(/v-[0–9]+/)([\S]+)([\S]+)” /><action type=”Rewrite” url=”{R:1}/{R:3}” /></rule></rules></rewrite></system.webServer>
After deploying to IIS first load
Version Information
Changing style file and deploying again
Version Information
<script src=”~/jquery.js?v=@(AppVersionNumber)”></script>

--

--

--

Technical Speaker | Computer Engineer | Full Stack Web Developer | ML Enthusiast | * Knowledge Shared = Knowledge² *

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Software by Design in a Functional Programming World

My DAX (re)Learning Path

Unable to switch directories in Azure AD

Basics of C Programming!

How to expose your PC to the internet without a public IP address?

AWS Quick Notes — Index only

Soft launching a new Microfrontend

Tall ship seen from a beach. Appears to be sailing into the sunset.

PHP 8 features I wish also existed in JavaScript

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Vaibhav Bhapkar

Vaibhav Bhapkar

Technical Speaker | Computer Engineer | Full Stack Web Developer | ML Enthusiast | * Knowledge Shared = Knowledge² *

More from Medium

Doomain — C# Model synchronization

Distributed Lock implementation using Zookeeper in .NET Core

Keyed Dependency Injection using .NET

Migrate ASP.Net core from 5.0 to 6.0