Related Tags:
Asp.Net It was developed by Microsoft to allow programmers to build dynamic web sites, web applications and web services. It was first released in January 2002 with version 1.0 of the .NET Framework, and is the successor to Microsoft's Active Server Pages (ASP) technology. Learn More, C# C# (pronounced "C-sharp") is an object-oriented programming language from Microsoft that aims to combine the computing power of C++ with the programming ease of Visual Basic. C# is based on C++ and contains features similar to those of Java. Learn More, ASP.NET MVC The ASP.NET MVC framework is a lightweight, highly testable presentation framework that (as with Web Forms-based applications) is integrated with existing ASP.NET features, such as master pages and membership-based authentication. The MVC framework is defined in the System.Web.Mvc assembly. Learn More, Bundling and Minification Bundling: It's a simple logical group of files that could be referenced by unique name and being loaded with one HTTP requestor. Minification: It's a process of removing unnecessary whitespace, line break and comments from code to reduce its size thereby improving load times. Learn More, IIS (Internet Information Services) IIS Stands for "Internet Information Services." IIS is a web server software package designed for Windows Server. It is used for hosting websites and other content on the Web. Microsoft's Internet Information Services provides a graphical user interface (GUI) for managing websites and the associated users. Learn More, Cache busting Cache busting solves the browser caching issue by using a unique file version identifier to tell the browser that a new version of the file is available. Therefore the browser doesn’t retrieve the old file from cache but rather makes a request to the origin server for the new file. Learn More, IIS URL Rewrite The URL Rewrite Module is an extension software for IIS (Internet Information Services). URLs should be created so that they are easy to remember for the users and easy to find for the search engines. The URL Rewrite Module enables web administrators to develop and implement rules that assist them in this task. Learn More, web.config A configuration file (web.config) is used to manage various settings that define a website. The settings are stored in XML files that are separate from your application code. ... Generally a website contains a single Web.config file stored inside the application root directory. Learn More,

how to implement Cache Busting technique of "File path versioning" or "File name versioning" for asp.net mvc site?

Description:I have studied about cache busting techniques and came to know that there are mainly three such techniques.
-File name versioning (e.g. style.v2.css)
-File path versioning (e.g. /v2/style.css)
-Query strings (e.g. style.css?version=2)
Currently I am using the query string for cache busting which is implemented by default in mvc's Bundling and Minification but the query string technique is causing performance issues, so I need to implement any of the other two technique.
Need help in this regard!!!

Posted by: | Posted on: Aug 24, 2018

1 answers

Replies

4

Hi,
There is a simple 4 steps implementation of the "File path versioning" cache busting technique as follows.
Step1:
Add the following lines of code in the web.config’s <system.webServer> element:

<staticcontent>
<clientcache cachecontrolmode="UseMaxAge" cachecontrolmaxage="365.00:00:00" />
</staticcontent>

Step2:
create a class "CacheBuster" with the method "GetVersionedPath" as shown in the following chunk of code.

using System;
using System.IO;
using System.Web;
using System.Web.Caching;
using System.Web.Hosting;

public class CacheBuster
{
public static string GetVersionedPath(string rootRelativePath)
{
if (HttpRuntime.Cache[rootRelativePath] == null)
{
string absolute = HostingEnvironment.MapPath("~" + rootRelativePath);
DateTime date = File.GetLastWriteTime(absolute);
int index = rootRelativePath.LastIndexOf('/');
string result = rootRelativePath.Insert(index, "/v-" + date.Ticks);
HttpRuntime.Cache.Insert(rootRelativePath, result, new CacheDependency(absolute));
}
return HttpRuntime.Cache[rootRelativePath] as string;
}
}

Step3
Call the method in such a way.

<link rel="stylesheet" href="@CacheBuster.GetVersionedPath("/content/Custom.css")" />

Step4:
Then by using the url rewrite module of IIS by adding the following snippet of XML to the web.config’s <system.webServer> section

<rewrite>
<rules>
<rule name="CacheBuster">
<match url="([\S]+)(/v-[0-9]+/)([\S]+)" />
<action type="Rewrite" url="{R:1}/{R:3}" />
</rule>
</rules>
</rewrite>

Full example can be seen in the article Implementation of cache busting technique in ASP.NET

Replied by: | Replied on: Aug 27, 2018



Reply
×

Code block Hyperlink bold Quotes block Upload Images

Preview