Class System.Net.Http.Headers.RangeConditionHeaderValue

namespace System.Net.Http.Headers

Summary

Represents a Range condition header value. This class is used to specify a condition that must be met for a range of data to be returned. It is typically used with the Range header and conditional request headers like If-Match, If-None-Match, If-Modified-Since, and If-Unmodified-Since.

The Range condition header can be constructed using either a date-time value or an entity tag value.

Constructors
Properties
Methods
Examples

Creating a RangeConditionHeaderValue with a Date


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

// Example date
DateTimeOffset lastModified = new DateTimeOffset(2023, 10, 27, 10, 30, 0, TimeSpan.Zero);

// Create the header value
RangeConditionHeaderValue condition = new RangeConditionHeaderValue(lastModified);

Console.WriteLine($"Condition: {condition}");
// Output: Condition: Tue, 27 Oct 2023 10:30:00 GMT
                    

Creating a RangeConditionHeaderValue with an Entity Tag


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

// Example entity tag
string etag = "\"some-etag-value\"";

// Create the header value
RangeConditionHeaderValue condition = new RangeConditionHeaderValue(etag);

Console.WriteLine($"Condition: {condition}");
// Output: Condition: "some-etag-value"
                    

Parsing a RangeConditionHeaderValue


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

string inputDate = "Tue, 27 Oct 2023 10:30:00 GMT";
if (RangeConditionHeaderValue.TryParse(inputDate, out RangeConditionHeaderValue conditionFromDate))
{
    Console.WriteLine($"Parsed Date Condition: {conditionFromDate.Date}");
    // Output: Parsed Date Condition: 10/27/2023 10:30:00 AM +00:00
}

string inputEtag = "\"another-etag\"";
if (RangeConditionHeaderValue.TryParse(inputEtag, out RangeConditionHeaderValue conditionFromEtag))
{
    Console.WriteLine($"Parsed ETag Condition: {conditionFromEtag.EntityTag}");
    // Output: Parsed ETag Condition: "another-etag"
}
                    
Remarks

This class is immutable, meaning its state cannot be changed after it is created. This ensures thread safety.

When using the RangeConditionHeaderValue with conditional HTTP requests, the server will return the requested resource only if the condition specified in the header is met. For example, if you use a date condition, the resource will be returned only if it has been modified since that date.