Represents a Range condition header value.
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.
Initializes a new instance of the RangeConditionHeaderValue class with a specified date.
Initializes a new instance of the RangeConditionHeaderValue class with a specified ETag.
Gets or sets the date for the condition.
Gets or sets the ETag for the condition.
Parses a string representation of a Range condition header value.
Tries to parse a string representation of a Range condition header value.
Initializes a new instance of the RangeConditionHeaderValue class using the specified date.
| Name | Type | Description |
|---|---|---|
date |
DateTimeOffset |
The date to use for the condition. |
Initializes a new instance of the RangeConditionHeaderValue class using the specified ETag.
| Name | Type | Description |
|---|---|---|
etag |
string |
The ETag to use for the condition. |
Gets or sets the date associated with the Range condition header. This is only valid if the condition is based on a date.
Gets or sets the ETag associated with the Range condition header. This is only valid if the condition is based on an ETag.
Parses a string representation of a Range condition header value.
| Name | Type | Description |
|---|---|---|
value |
string |
The string to parse. |
RangeConditionHeaderValue: A RangeConditionHeaderValue instance.
FormatException: The string is not a valid Range condition header value.
Tries to parse a string representation of a Range condition header value.
| Name | Type | Description |
|---|---|---|
value |
string |
The string to parse. |
parsedValue |
out RangeConditionHeaderValue |
When this method returns, contains the parsed RangeConditionHeaderValue instance if the parsing succeeded, or null if the parsing failed. |
bool: true if the string was parsed successfully; otherwise, false.
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)
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"
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.");
}
}