Search code examples
c#httpclienthttp-caching

System.Net.Http.HttpClient Disable Caching (.Net Standart Project)


In my .NET Standard project I'm using System.Net.Http.HttpClient. How can I disable all caching (request caching especially) in HttpClient?

If server sends responses with no cache header problem solves. But I want to make this on client side. I want to completely disable all caching.

Thanks.

Edit: It looks like I could use WebRequestHandler but this does not exist in .NET standard. I can only use HttpClientHandler but HttpClientHandler doesn't have any option about caching.


Solution

  • You can use CacheControlHeaderValue in HttpClient

    using System.Net.Http.Headers;
    
    httpClient.DefaultRequestHeaders.CacheControl = new CacheControlHeaderValue
    {
      NoCache = true
    }
    

    For more information you can look https://learn.microsoft.com/en-us/dotnet/api/system.net.http.headers.cachecontrolheadervalue