ContentRangeHeaderValue Class
Represents the value of a Content-Range header.
Namespace
Assembly
System.Net.Http.dll
Syntax
public sealed class ContentRangeHeaderValue : ICloneable
Remarks
The Content-Range header is used to indicate the range of the entity-body that is being returned in a response to a byte-range request. This header is commonly used for partial content delivery, such as resuming interrupted downloads.
A Content-Range header can be structured as follows:
Content-Range: <unit> <range-start>-<range-end>/<size>
<unit>: The unit of data, typically "bytes".<range-start>: The starting byte of the range.<range-end>: The ending byte of the range.<size>: The total size of the entity-body. This can be an asterisk (*) if the size is unknown.
Content-Range: bytes 0-499/1000 indicates that the response contains bytes 0 through 499 of a total of 1000 bytes. A value of Content-Range: bytes */1000 indicates that the total size is 1000 bytes, but the current response does not contain a specific range.
Constructors
ContentRangeHeaderValue(long, long)
Initializes a new instance of the ContentRangeHeaderValue class with the specified start and end byte positions.
public ContentRangeHeaderValue(long from, long to);
Parameters
from- The starting byte position of the range.
to- The ending byte position of the range.
ContentRangeHeaderValue(long, long, long)
Initializes a new instance of the ContentRangeHeaderValue class with the specified start, end byte positions, and the total size of the entity-body.
public ContentRangeHeaderValue(long from, long to, long length);
Parameters
from- The starting byte position of the range.
to- The ending byte position of the range.
length- The total size of the entity-body.
Properties
From
Gets the starting byte position of the range.
public long? From { get; }
Length
Gets the total size of the entity-body. If the size is unknown, this value is null.
public long? Length { get; }
To
Gets the ending byte position of the range.
public long? To { get; }
Methods
Parse(string)
Parses a string representation of a Content-Range header value.
public static ContentRangeHeaderValue Parse(string input);
Parameters
input- A string representation of a
Content-Rangeheader value.
Returns
ContentRangeHeaderValue class.Exceptions
ArgumentInvalidOperationException: The string is not a valid Content-Range header value.TryParse(string, out ContentRangeHeaderValue)
Tries to parse a string representation of a Content-Range header value.
public static bool TryParse(string input, out ContentRangeHeaderValue value);
Parameters
input- A string representation of a
Content-Rangeheader value. value- When this method returns, contains the parsed
ContentRangeHeaderValueobject if the parse was successful, ornullif the parse failed.
Returns
true if the string was parsed successfully; otherwise, false.ToString()
Returns the string representation of the Content-Range header value.
public override string ToString();
Returns
Content-Range header value.Examples
Creating a Content-Range Header
// Example 1: Partial content with known total size var range1 = new ContentRangeHeaderValue(0, 499, 1000); Console.WriteLine(range1.ToString()); // Output: bytes 0-499/1000 // Example 2: Entire content, size unknown var range2 = new ContentRangeHeaderValue(0, 999); range2.Length = 1000; // Setting length separately if not provided initially Console.WriteLine(range2.ToString()); // Output: bytes 0-999/1000 // Example 3: Unknown total size (using *) var range3 = new ContentRangeHeaderValue(500, 999); range3.Length = null; // Indicates unknown size with '*' Console.WriteLine(range3.ToString()); // Output: bytes 500-999/*
Parsing a Content-Range Header
string headerString = "bytes 100-199/500"; ContentRangeHeaderValue parsedRange; if (ContentRangeHeaderValue.TryParse(headerString, out parsedRange)) { Console.WriteLine($"Parsed From: {parsedRange.From}"); // Output: Parsed From: 100 Console.WriteLine($"Parsed To: {parsedRange.To}"); // Output: Parsed To: 199 Console.WriteLine($"Parsed Length: {parsedRange.Length}"); // Output: Parsed Length: 500 } else { Console.WriteLine($"Failed to parse: {headerString}"); } // Example with unknown size string headerStringUnknown = "bytes */1000"; if (ContentRangeHeaderValue.TryParse(headerStringUnknown, out parsedRange)) { Console.WriteLine($"Parsed From: {parsedRange.From}"); // Output: Parsed From: null Console.WriteLine($"Parsed To: {parsedRange.To}"); // Output: Parsed To: null Console.WriteLine($"Parsed Length: {parsedRange.Length}"); // Output: Parsed Length: 1000 }