Class PartialObjectResult
- Namespace
- FunctionalDdd
- Assembly
- FunctionalDdd.Asp.dll
Represents an ObjectResult that returns HTTP 206 Partial Content with a Content-Range header. Used to indicate that the response contains a subset of the requested resource.
public class PartialObjectResult : ObjectResult, IStatusCodeActionResult, IActionResult
- Inheritance
-
PartialObjectResult
- Implements
- Inherited Members
- Extension Methods
Examples
Using PartialObjectResult directly in a controller:
[HttpGet]
public IActionResult GetUsers([FromQuery] int page = 0, [FromQuery] int pageSize = 25)
{
var from = page * pageSize;
var to = from + pageSize - 1;
var totalCount = _userRepository.Count();
var users = _userRepository.GetRange(from, pageSize);
if (to >= totalCount - 1)
{
// Complete result - return 200 OK
return Ok(users);
}
// Partial result - return 206 Partial Content
return new PartialObjectResult(from, to, totalCount, users);
}
// Response headers:
// HTTP/1.1 206 Partial Content
// Content-Range: items 0-24/100
Using with ActionResultExtensions (recommended approach):
[HttpGet]
public ActionResult<IEnumerable<UserDto>> GetUsers(
[FromQuery] int page = 0,
[FromQuery] int pageSize = 25)
{
var from = page * pageSize;
var to = from + pageSize - 1;
return _userService
.GetPagedUsers(from, pageSize)
.Map(result => (result.Users, result.TotalCount))
.Map(x => x.Users)
.ToActionResult(this, from, to, totalCount);
// Automatically returns PartialObjectResult when appropriate
}
Remarks
This class implements the HTTP 206 Partial Content response as defined in RFC 7233. It's commonly used for:
- Paginated API responses
- Range requests for large datasets
- Incremental data loading in client applications
- Resumable downloads or data transfers
The Content-Range header format is: {unit} {from}-{to}/{total}
This class is typically used by ToActionResult<TValue>(Result<TValue>, ControllerBase, long, long, long) and should rarely be instantiated directly in controller code.
Constructors
PartialObjectResult(long, long, long?, object?)
Initializes a new instance of the PartialObjectResult class with explicit range values.
public PartialObjectResult(long rangeStart, long rangeEnd, long? totalLength, object? value)
Parameters
rangeStartlongThe starting index of the range (zero-indexed, inclusive).
rangeEndlongThe ending index of the range (zero-indexed, inclusive).
totalLengthlong?The total number of items available, or null if unknown.
valueobjectThe partial data to include in the response body.
Examples
// Return items 0-24 out of 100 total
return new PartialObjectResult(0, 24, 100, users);
// Content-Range: items 0-24/100
// Return items 25-49, total unknown
return new PartialObjectResult(25, 49, null, users);
// Content-Range: items 25-49/*
Remarks
The range is inclusive on both ends: [rangeStart, rangeEnd]. For example, to return items 0-24 of 100: rangeStart=0, rangeEnd=24, totalLength=100.
The Content-Range header will be set to: items {rangeStart}-{rangeEnd}/{totalLength}
If totalLength is null, it will be formatted as: items {rangeStart}-{rangeEnd}/*
The unit "items" is used by default and is suitable for most paginated API responses. For byte-range requests (e.g., file downloads), use the constructor that accepts ContentRangeHeaderValue directly.
PartialObjectResult(ContentRangeHeaderValue, object?)
Initializes a new instance of the PartialObjectResult class with a pre-configured ContentRangeHeaderValue.
public PartialObjectResult(ContentRangeHeaderValue contentRangeHeaderValue, object? value)
Parameters
contentRangeHeaderValueContentRangeHeaderValueThe Content-Range header value to use in the response.
valueobjectThe partial data to include in the response body.
Examples
Using with byte ranges for file downloads:
var contentRange = new ContentRangeHeaderValue(0, 1023, 2048)
{
Unit = "bytes"
};
return new PartialObjectResult(contentRange, fileBytes);
// Content-Range: bytes 0-1023/2048
Using with a domain model that includes range information:
var pagedResult = await _repository.GetPagedAsync(page, pageSize);
var contentRange = new ContentRangeHeaderValue(
pagedResult.From,
pagedResult.To,
pagedResult.TotalCount)
{
Unit = "items"
};
return new PartialObjectResult(contentRange, pagedResult.Items);
Remarks
This constructor allows full control over the Content-Range header, including:
- Custom unit values (e.g., "bytes" for file downloads, "items" for collections)
- Pre-computed range values from domain models
- Complex range scenarios not covered by the simpler constructor
Use this constructor when you need to specify a unit other than "items", or when the Content-Range information is already available as a ContentRangeHeaderValue.
Properties
ContentRangeHeaderValue
Gets the Content-Range header value that will be included in the response.
public ContentRangeHeaderValue ContentRangeHeaderValue { get; }
Property Value
- ContentRangeHeaderValue
The ContentRangeHeaderValue containing range and total length information.
Remarks
This property can be used to inspect the range information before the response is sent, for example in middleware or filters.
Methods
OnFormatting(ActionContext)
Called during the formatting of the action result to add the Content-Range header to the HTTP response.
public override void OnFormatting(ActionContext context)
Parameters
contextActionContextThe action context containing the HTTP context and response.
Remarks
This method is called by the ASP.NET Core framework as part of the response formatting pipeline. It adds the Content-Range header to the response headers before the response is sent to the client.
The Content-Range header format follows RFC 7233:
Content-Range: {unit} {from}-{to}/{total}
You should not need to call this method directly; it's invoked automatically by the framework.
Exceptions
- ArgumentNullException
Thrown if
contextis null.