CacheControlHeaderValue Class

Represents a Cache-Control header value.

Syntax

[SerializableAttribute]
public sealed class CacheControlHeaderValue

Fields

This class does not contain any fields.

Constructors

CacheControlHeaderValue()

public CacheControlHeaderValue()
Initializes a new instance of the CacheControlHeaderValue class.

Properties

MaxAge

public TimeSpan? MaxAge { get; set; }
Gets or sets the value of the max-age directive.

MaxStale

public TimeSpan? MaxStale { get; set; }
Gets or sets the value of the max-stale directive.

MinFresh

public TimeSpan? MinFresh { get; set; }
Gets or sets the value of the min-fresh directive.

MustRevalidate

public bool MustRevalidate { get; set; }
Gets or sets a value that indicates whether the must-revalidate directive is present.

NoCache

public bool NoCache { get; set; }
Gets or sets a value that indicates whether the no-cache directive is present.

NoStore

public bool NoStore { get; set; }
Gets or sets a value that indicates whether the no-store directive is present.

NoTransform

public bool NoTransform { get; set; }
Gets or sets a value that indicates whether the no-transform directive is present.

Public

public bool Public { get; set; }
Gets or sets a value that indicates whether the public directive is present.

SharedCache

public bool SharedCache { get; set; }
Gets or sets a value that indicates whether the s-maxage directive is present.

Private

public bool Private { get; set; }
Gets or sets a value that indicates whether the private directive is present.

Extensions

public IList<NameValueHeaderValue> Extensions { get; }
Gets a collection of custom Cache-Control extensions.

Methods

AddExtensions(IEnumerable<NameValueHeaderValue> extensions)

public void AddExtensions(IEnumerable<NameValueHeaderValue> extensions)
Adds custom Cache-Control extensions to the header.

AddExtension(NameValueHeaderValue extension)

public void AddExtension(NameValueHeaderValue extension)
Adds a custom Cache-Control extension to the header.

Parse(string value)

public static CacheControlHeaderValue Parse(string value)
Parses a Cache-Control header value.
Note: This static method is used to parse a string that represents a Cache-Control header.

TryParse(string value, out CacheControlHeaderValue parsedValue)

public static bool TryParse(string value, out CacheControlHeaderValue parsedValue)
Tries to parse a Cache-Control header value.
Parameter Description value The string to parse. parsedValue When this method returns, contains the parsed CacheControlHeaderValue if the parse succeeded, or null if the parse failed.
Note: This static method is used to parse a string that represents a Cache-Control header without throwing an exception on failure.

ToString()

public override string ToString()
Returns the string representation of the Cache-Control header.

Examples

Example 1: Creating a CacheControlHeaderValue

using System;
using System.Net.Http.Headers;

public class Example
{
    public static void Main(string[] args)
    {
        var cacheControl = new CacheControlHeaderValue();
        cacheControl.MaxAge = TimeSpan.FromSeconds(600);
        cacheControl.NoCache = true;
        cacheControl.Public = true;

        Console.WriteLine(cacheControl.ToString());
        // Output: max-age=600, no-cache, public
    }
}
        
Example 2: Parsing a CacheControlHeaderValue

using System;
using System.Net.Http.Headers;

public class Example
{
    public static void Main(string[] args)
    {
        string headerValue = "no-store, max-age=3600, s-maxage=7200";
        if (CacheControlHeaderValue.TryParse(headerValue, out var cacheControl))
        {
            Console.WriteLine($"NoStore: {cacheControl.NoStore}");
            Console.WriteLine($"MaxAge: {cacheControl.MaxAge}");
            Console.WriteLine($"SharedCache (s-maxage): {cacheControl.SharedCache}"); // SharedCache represents s-maxage
        }
        else
        {
            Console.WriteLine("Failed to parse Cache-Control header.");
        }
    }
}
        
Example 3: Using Extensions

using System;
using System.Collections.Generic;
using System.Net.Http.Headers;

public class Example
{
    public static void Main(string[] args)
    {
        var cacheControl = new CacheControlHeaderValue();
        cacheControl.Extensions.Add(new NameValueHeaderValue("private", "session"));
        cacheControl.Extensions.Add(new NameValueHeaderValue("immutable"));

        Console.WriteLine(cacheControl.ToString());
        // Output: private="session", immutable
    }
}