System.Net.Http.Headers.RangeConditionHeaderValue

In: System.Net.Http.Headers   |   Namespace: System.Net.Http.Headers
🏛️

RangeConditionHeaderValue

Represents a Range condition header value.

Class RangeConditionHeaderValue

This class represents a Range condition header value, which is used in HTTP requests to specify a byte range within a resource. It typically consists of a date or an ETag value and a comparison operator.

Range condition headers are commonly used in conjunction with caching mechanisms or for partial content retrieval. For example, an If-Modified-Since header uses a date condition, while an If-Match or If-None-Match header uses an ETag condition.

The RangeConditionHeaderValue class encapsulates these conditions, providing a structured way to parse and represent them.

Constructors

Properties

Methods

Members

Constructors

Properties

Methods

Examples

Creating a RangeConditionHeaderValue with a Date


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

// Create a date-based condition for a resource modified after a specific date.
DateTimeOffset lastModified = new DateTimeOffset(2023, 10, 26, 10, 0, 0, TimeSpan.Zero);
RangeConditionHeaderValue dateCondition = new RangeConditionHeaderValue(lastModified);

Console.WriteLine($"Date Condition: {dateCondition}");
// Output might look like: Date Condition:  (format may vary)
                

Creating a RangeConditionHeaderValue with an ETag


using System.Net.Http.Headers;

// Create an ETag-based condition for a resource.
string etagValue = "\"abcde12345\"";
RangeConditionHeaderValue etagCondition = new RangeConditionHeaderValue(etagValue);

Console.WriteLine($"ETag Condition: {etagCondition}");
// Output: ETag Condition: "abcde12345"
                

Parsing a RangeConditionHeaderValue


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

string httpHeaderValue = "If-Modified-Since: Tue, 15 Nov 1994 12:45:26 GMT";
string[] parts = httpHeaderValue.Split(':');
if (parts.Length > 1)
{
    string conditionString = parts[1].Trim();
    if (RangeConditionHeaderValue.TryParse(conditionString, out RangeConditionHeaderValue parsedCondition))
    {
        Console.WriteLine($"Parsed Condition: {parsedCondition}");
        if (parsedCondition.Date.HasValue)
        {
            Console.WriteLine($"  Condition Type: Date");
            Console.WriteLine($"  Date: {parsedCondition.Date.Value}");
        }
        else if (!string.IsNullOrEmpty(parsedCondition.Etag))
        {
            Console.WriteLine($"  Condition Type: ETag");
            Console.WriteLine($"  ETag: {parsedCondition.Etag}");
        }
    }
    else
    {
        Console.WriteLine($"Failed to parse '{conditionString}' as a RangeConditionHeaderValue.");
    }
}