RangeConditionHeaderValue Class

Represents a conditional range request header value. This class is used to construct conditional Range headers, such as If-Range.

Description

The RangeConditionHeaderValue class in the System.Net.Http.Headers namespace provides a way to specify conditions for retrieving a range of data from a resource. This is commonly used in HTTP requests to support partial content retrieval, allowing clients to resume interrupted downloads or fetch specific portions of a file.

It typically works in conjunction with the Range header and uses entities like ETag or Last-Modified dates to ensure that the requested range is still valid.

Syntax

public sealed class RangeConditionHeaderValue

Methods

ctor

public RangeConditionHeaderValue()

Initializes a new instance of the RangeConditionHeaderValue class.

ctor

public RangeConditionHeaderValue(DateTimeOffset date)

Initializes a new instance of the RangeConditionHeaderValue class with a date.

date: DateTimeOffset - The date used for the condition.

ctor

public RangeConditionHeaderValue(string etag)

Initializes a new instance of the RangeConditionHeaderValue class with an ETag.

etag: string - The ETag used for the condition.

ctor

public RangeConditionHeaderValue(string etag, DateTimeOffset date)

Initializes a new instance of the RangeConditionHeaderValue class with an ETag and a date.

etag: string - The ETag used for the condition. date: DateTimeOffset - The date used for the condition.

Parse

public static RangeConditionHeaderValue Parse(string value)

Parses a range condition header value from a string.

value: string - The string to parse.

TryParse

public static bool TryParse(string value, out RangeConditionHeaderValue parsedValue)

Tries to parse a range condition header value from a string.

value: string - The string to parse. parsedValue: out RangeConditionHeaderValue - When this method returns, contains the parsed value or null if parsing failed.

Properties

Date

public DateTimeOffset? Date { get; }

Gets the date used for the range condition.

ETag

public string ETag { get; }

Gets the ETag used for the range condition.

Usage Example

Here's how you might use RangeConditionHeaderValue to create an If-Range header:


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

var request = new HttpRequestMessage(HttpMethod.Get, "https://example.com/resource");

// Example 1: Using ETag
string currentETag = "\"abcdef123456\"";
request.Headers.IfRange = new RangeConditionHeaderValue(currentETag);

// Example 2: Using Last-Modified date
DateTimeOffset lastModifiedDate = new DateTimeOffset(2023, 10, 27, 10, 30, 0, TimeSpan.Zero);
request.Headers.IfRange = new RangeConditionHeaderValue(lastModifiedDate);

// The request.Headers.IfRange will be serialized to the If-Range header.
// For Example 1, it would be: If-Range: "abcdef123456"
// For Example 2, it would be: If-Range: Fri, 27 Oct 2023 10:30:00 GMT