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
Initializes a new instance of the RangeConditionHeaderValue class.
ctor
Initializes a new instance of the RangeConditionHeaderValue class with a date.
DateTimeOffset - The date used for the condition.
ctor
Initializes a new instance of the RangeConditionHeaderValue class with an ETag.
string - The ETag used for the condition.
ctor
Initializes a new instance of the RangeConditionHeaderValue class with an ETag and a date.
string - The ETag used for the condition.
date: DateTimeOffset - The date used for the condition.
Parse
Parses a range condition header value from a string.
string - The string to parse.
TryParse
Tries to parse a range condition header value from a string.
string - The string to parse.
parsedValue: out RangeConditionHeaderValue - When this method returns, contains the parsed value or null if parsing failed.
Properties
Date
Gets the date used for the range condition.
ETag
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