Implement HTTP caching in ASP.NET Web API


Caching in ASP.NET Web API

To improve performance, we need to do caching. Caching reduces the server overhead and often doesn’t even required you to hit the API if the cache resource version is available. It is also a REST architecture constraint. This article explains the implementation of HTTP caching in ASP.NET Web API using ETag.

How do we implement caching in Web API ?

We can implement caching using ETag, called HTTP caching. ETag is a magic string that says, you asked for the employee , and this ETag represents this one employee as it exists at the time it was retrieved from the server. The benefit here is that later when you make a subsequent request, you are going to include the ETag in the header to tell the server, hey , if you already have this version, tell me, because I have the ETag, therefore you don’t actually have to return it to me.

Caching in ASP.NET Web API

In the preceding diagram you can see the client and server communication with ETag header. First client makes a request to our API and API respond 200 ok response with employee details and this response also includes a ETag to identify the version of the resource. For the subsequent request client includes header IF-NONE-MATCH with ETag, the ETag that received before. Server will check the ETag against the previous ETag. If both are same, server return 304 not modified status code.

To implement this, add new package cachecow.server and add the cache handler in the WebApiConfig.cs file. In the following code you can see the cache handler in the last line.

public static class WebApiConfig
        public static void Register(HttpConfiguration config)
            // Web API configuration and services
            // Web API routes
                name: "DefaultApi",
               routeTemplate: "api/{controller}/{action}/{id}",
                defaults: new { id = RouteParameter.Optional }
            config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
            config.MessageHandlers.Add(new CacheCow.Server.CachingHandler(config));

Then go-ahead and run the application. Here I’m using fiddler to interact with the API. Enter our API URL in fiddler and click on execute button. You can see the 200 ok response with ETag in the header.

API caching header fiddler

Copy the ETag , enter into composer tab and hit execute. Server will check the ETag against the previous ETag. If both are same, server return 304 not modified status code

API caching enter ETag

You can see the 304 not modified result in the following screen.

API caching 304

Server would not return any Json/Xml result for 304 response. The response will be blank.

The following two tabs change content below.

Tom Mohan

Tom Mohan is a technologist who loves to code and build. He enjoys working on Microsoft Technologies. Tom specializes in ASP.NET MVC, Web API , Azure, C# ,WPF, SQL etc and holds a Bachelor engineering degree in Computer Science. Certification : MCSD , MCTS

Latest posts by Tom Mohan (see all)