.NET API Reference

System.Net.Http.Headers

RangeConditionHeaderValue

Represents a conditional range request header value.

Conditional range requests allow a client to request only a portion of a resource. This class is used in conjunction with the Range header to specify conditions that must be met for the partial response to be sent.

Remarks

The RangeConditionHeaderValue class supports two types of conditions:

  • ETag-based condition: Checks if the ETag of the resource matches a given value.
  • Last-Modified-based condition: Checks if the last modified date of the resource matches a given date.

This class is used by the Range property of the HttpRequestHeaders class.

Constructors

Properties

Methods

Examples

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

public class Example
{
    public static void Main()
    {
        // Create a range condition header value based on an ETag
        RangeConditionHeaderValue etagCondition = new RangeConditionHeaderValue("\"abcdef\"");
        Console.WriteLine($"ETag Condition: {etagCondition}");

        // Create a range condition header value based on a date
        DateTimeOffset lastModified = new DateTimeOffset(2023, 10, 27, 10, 30, 0, TimeSpan.Zero);
        RangeConditionHeaderValue dateCondition = new RangeConditionHeaderValue(lastModified);
        Console.WriteLine($"Date Condition: {dateCondition}");

        // Using TryParse
        RangeConditionHeaderValue parsedCondition;
        if (RangeConditionHeaderValue.TryParse("\"xyz123\"", out parsedCondition))
        {
            Console.WriteLine($"Successfully parsed: {parsedCondition}");
        }
        else
        {
            Console.WriteLine("Failed to parse.");
        }
    }
}

See Also