There are approximately 2 ways to implement caching in the ASP. One is through the ETag, one by outputcache similar to ASP.
Cache with ETag
Install Cachecow.server First
Install-package Cachecow.server
In the Webapiconfig.
Public Static classwebapiconfig{ Public StaticHttpconfiguraiton Register () {varConfig =Newhttpconfiguration (); //support for setting routes via attributesCONFIG. Maphttpattributeroutes (); Config. Routes.maphttproute ("defaultrouting", "Api/{controller}/{id}", defaults:New{id =routeparamter.optional}); //CONFIG. Formatters.JsonFormatter.SupportedMediaTypes. ADD (NewMediatypeheadervalue ("text/html")); Config. Formatters.XmlFormatter.SupportedMediaType.Clear (); Config. FORAMTTERS.JSONFORMATTER.SUPPOORTEDMEDIATYPES.ADD (NewMediatypeheadervalue ("Application/json-patch+json"); ); Config. Formatters.JsonFormatter.SerializerSettings.Formatting=Newtonsoft.Json.Formatting.Indented; Config. Formatters.JsonFormatter.SerializerSettings.ContractResolver=NewCamelcaseproeprtynamescontractresolver (); //HTTP cache is cached in memory by defaultConfig. Messagehandlers.add (NewCacheCow.Server.CachingHandler (config)); returnconfig; }}
→ Client makes a request
GET HTTP://LOCALHOST:43321/API/GROUPS/1
→ Returns the 200 status code in the headers of the response:
etag:w/"..."
Last-modified: ...
→ Request again, pass the ETag with the If-none-match attribute.
GET HTTP://LOCALHOST:43321/API/GROUPS/1
host:localhost:43321
if-none-match:etag:w/""
→ Return 304 Status code
Implementing caching through OutputCache
Another way to implement caching in the ASP. NET Web API is to use outputcache similar to ASP. NET. NET MVC: Strathweb.CacheOutput.WebApi2
A summary is also made about the ASP. NET Web API cache, which is implemented in the ETag implementation cache in the ASP.
2 ways the ASP. NET Web API implements caching