ASP.NET Hosting

ASP.NET Core 8 Tutorial: Caching of Responses in.NET Web API

It is a technique for caching the responses of an API so that they can be served to subsequent requests more quickly. Responses are stored with a key that identifies them uniquely; the Cache has a limited capacity and a policy for removing items when it is filled.

The Advantages of Response Caching

Enhanced server performance by reducing server burden.
Reduced server burden, as the cached response is served rather than a new one being generated.
Reduced bandwidth utilization decreases the quantity of data that must be transferred between the server and the client.

It can reduce the number of requests that reach the server, decreasing the likelihood of certain attacks.

On which Requests can Request Caching be applied?

  • Get
  • Head

Restrictions on Response Caching

  • The server must respond to the request with a 200 (OK) status code.
  • Response Caching Middleware must be inserted before caching-required middleware.
  • The Authorization prefix is prohibited.
  • The parameters of the Cache-Control header must be genuine, and the response must be marked as public and not private.
  • If present, the Content-Length header value must match the extent of the response body.

Examples of Response Caching in the Real World?

  • Website devoted to delivering news
  • Commerce-based websites

How Is It Implemented?

// Configure Response Cache Middleware in .Net 6
var builder = WebApplication.CreateBuilder(args);

var app = builder.Build()

How to apply Cache on the Method Level?

[ResponseCache(Duration =60,Location =ResponseCacheLocation.Client)]
public ActionResult<string> Get(int id)
  return "value";

How to apply Cache on ControllerLevel?

[ResponseCache(Duration = 60, Location = ResponseCacheLocation.Client)]
public class ValuesController : ControllerBase
    // GET api/values
    public ActionResult<IEnumerable<string>> Get()
        return new string[] { "value1", "value2" };

How Can We Verify It?

  • Create an application, apply to cache, and then request it from Postman and set the time to 60 minutes; you will notice that only the first request will reach the controller; after that, even if you try request will not reach the controller.

ASP.NET Core 8 Hosting Recommendation is a popular recommendation that offers various hosting choices. Starting from shared hosting to dedicated servers, you will find options fit for beginners and popular websites. It offers various hosting choices if you want to scale up. Also, you get flexible billing plans where you can choose to purchase a subscription even for one or six months.