2 ways the ASP. NET Web API implements caching

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.