ContentRangeHeaderValue Class

[System.Net.Http.Headers] public class ContentRangeHeaderValue

Represents the value of the Content-Range header.

The Content-Range header is used with a range query by a client to indicate which part of a document is being returned by the server. It can also be used by the server to indicate that the document is not fully satisfying a partial range request.

Constructors

ContentRangeHeaderValue(long from, long to)

[System.Net.Http.Headers] public ContentRangeHeaderValue(long from, long to)

Initializes a new instance of the ContentRangeHeaderValue class with a specified range of bytes.

ContentRangeHeaderValue(long from, long to, long length)

[System.Net.Http.Headers] public ContentRangeHeaderValue(long from, long to, long length)

Initializes a new instance of the ContentRangeHeaderValue class with a specified range of bytes and the total length of the content.

ContentRangeHeaderValue(long length)

[System.Net.Http.Headers] public ContentRangeHeaderValue(long length)

Initializes a new instance of the ContentRangeHeaderValue class with a specified total length of the content.

Properties

From

[System.Net.Http.Headers] public long? From { get; }

Gets the start byte of a range of bytes.

Length

[System.Net.Http.Headers] public long? Length { get; }

Gets the total length of the content.

Ranges

[System.Net.Http.Headers] public IEnumerable<RangeItemHeaderValue> Ranges { get; }

Gets the ranges of bytes.

To

[System.Net.Http.Headers] public long? To { get; }

Gets the end byte of a range of bytes.

Methods

Equals(object obj)

[System.Net.Http.Headers] public override bool Equals(object obj)

Determines whether the specified object is equal to the current object.

GetHashCode()

[System.Net.Http.Headers] public override int GetHashCode()

Serves as the default hash function.

Parse(string input)

[System.Net.Http.Headers] public static ContentRangeHeaderValue Parse(string input)

Parses a Content-Range header string.

ToString()

[System.Net.Http.Headers] public override string ToString()

Returns a string that represents the current object.

TryParse(string input, out ContentRangeHeaderValue parsedValue)

[System.Net.Http.Headers] public static bool TryParse(string input, out ContentRangeHeaderValue parsedValue)

Tries to parse a Content-Range header string.

Examples

Creating a Content-Range header:
var range = new ContentRangeHeaderValue(100, 200, 1000);
// range.ToString() will produce: "bytes 100-200/1000"

var invalidRange = new ContentRangeHeaderValue(100, 200);
// invalidRange.ToString() will produce: "bytes 100-200/*"

var totalLength = new ContentRangeHeaderValue(5000);
// totalLength.ToString() will produce: "bytes */5000"
Parsing a Content-Range header:
string headerString = "bytes 0-499/12345";
ContentRangeHeaderValue parsedRange;
if (ContentRangeHeaderValue.TryParse(headerString, out parsedRange))
{
    Console.WriteLine($"From: {parsedRange.From}");      // Output: From: 0
    Console.WriteLine($"To: {parsedRange.To}");          // Output: To: 499
    Console.WriteLine($"Length: {parsedRange.Length}");  // Output: Length: 12345
}
else
{
    Console.WriteLine($"Failed to parse: {headerString}");
}