skip to content

How do you serve a resumable video download from an ASP.NET Core 10 controller with PhysicalFile and range requests?

level: middleimportance: should knowfreq 40%

answer

  1. enableRangeProcessing is off by default
  2. Accept-Ranges: bytes, 206 Partial Content
  3. PhysicalFile needs a rooted path
  4. File("path") looks in wwwroot
  5. If-Range with Last-Modified or ETag

basics

~10 s

Return PhysicalFile(absolutePath, "video/mp4", enableRangeProcessing: true). ASP.NET Core then advertises Accept-Ranges: bytes and answers Range requests with 206 Partial Content, or 416 when unsatisfiable. Range processing is off by default.

solid answer

~40 s

Resumable playback and downloads rely on HTTP range requests. In a controller, `PhysicalFile(path, "video/mp4", enableRangeProcessing: true)` streams the file from disk and, because `EnableRangeProcessing` is `true`, adds `Accept-Ranges: bytes`, answers `Range: bytes=...` on GET and HEAD with `206 Partial Content` and a `Content-Range` header, and returns `416` for a range outside the file. `PhysicalFile` also sends `Last-Modified` from the file, so `If-Range` validates resumes; pass an `EntityTagHeaderValue` for ETag-based checks. The path must be absolute, and a missing file throws `FileNotFoundException`, so check existence and return `NotFound()` first. `File("videos/x.mp4", ...)` with a string resolves against the web root instead, and `File(byte[])` loads the whole video into memory. A `FileStreamResult` supports ranges only when its stream can seek.

code

http · 8 lines
http
GET /api/recordings/3f2b9c1e-8a4d-4f7b-9c2e-5d1a6b7c8e9f HTTP/1.1
Range: bytes=0-1048575

HTTP/1.1 206 Partial Content
Accept-Ranges: bytes
Content-Range: bytes 0-1048575/734003200
Content-Length: 1048576
Content-Type: video/mp4

go deeper

for a junior

Recall PhysicalFile with enableRangeProcessing: true, and that the response is 206 for a range request.

for a middle

Explain the result types, why PhysicalFile needs an absolute path, how File(string) differs, and how If-Range and Last-Modified interact.

for a senior

Serve large media efficiently and safely: authorization per request, stable ETags for replaced files, and no byte-array or non-seekable stream results.

for a principal

Decide when media should be served by the app at all versus a CDN or storage service with signed URLs, weighing control against cost.

## What resumable means in HTTP In the telehealth app, patients replay recorded consultations. Video players seek by asking for byte ranges, and download managers resume with `Range: bytes=52428800-`. The server must advertise `Accept-Ranges: bytes`, answer `206 Partial Content` with a `Content-Range` header, and refuse impossible ranges with `416 Range Not Satisfiable`. ## The controller action ```csharp [HttpGet("api/recordings/{id:guid}")] [Authorize] public IActionResult GetRecording(Guid id) { var path = Path.Combine(_recordingsRoot, $"{id:N}.mp4"); if (!System.IO.File.Exists(path)) return NotFound(); return PhysicalFile(path, "video/mp4", enableRangeProcessing: true); } ``` Note `System.IO.File`: inside a controller, `File` alone refers to `ControllerBase.File`. ## What ASP.NET Core does with the result The file result executors share one implementation of the HTTP rules: 1. It sets `Last-Modified` (for `PhysicalFile`, from the file when you pass none) and `ETag` if you supplied an `EntityTagHeaderValue`. 2. It evaluates conditional headers first: `If-None-Match` or `If-Modified-Since` can produce **304**, `If-Match` or `If-Unmodified-Since` **412**. 3. If `EnableRangeProcessing` is `true` and the length is known, it adds `Accept-Ranges: bytes`. 4. For a **GET or HEAD** with a valid `Range` header, and an `If-Range` that still matches the current `Last-Modified` or `ETag`, it sends **206** with only the requested bytes; an unsatisfiable range gets **416**. 5. If `If-Range` no longer matches (the file changed), it sends the full file with **200**, so the client does not stitch two versions together. When `EnableRangeProcessing` is `false`, the default, the `Range` header is ignored and every request gets the full 200 response; the executor only logs that range processing is not enabled. ## Choosing the right result type | Helper | Result | Range support | Trap | |---|---|---|---| | `PhysicalFile(path, type, enableRangeProcessing: true)` | `PhysicalFileResult` | yes | path must be rooted; missing file throws | | `File("relative/path", type)` | `VirtualFileResult` | yes, with the flag | resolves against the web root file provider | | `File(stream, type, enableRangeProcessing: true)` | `FileStreamResult` | only for seekable streams | a non-seekable stream has no known length | | `File(bytes, type)` | `FileContentResult` | yes, with the flag | the whole file sits in memory | For large videos on local disk, `PhysicalFile` is the natural choice: its executor calls `HttpResponse.SendFileAsync`, which streams the file (or just the requested range) from disk in chunks instead of loading it into memory. ## HEAD and multi-range requests - A **HEAD** request goes through the same logic: `Content-Length`, `Accept-Ranges` and the range headers are set, but no body is sent. - A `Range` header asking for **several ranges** at once (`bytes=0-99,200-299`) is not supported: the range helper logs "Multiple ranges are not supported." at Debug level, ignores the header and serves the full file with 200. Video players ask for one range at a time, so this rarely matters. - A range whose start lies beyond the end of the file is the unsatisfiable case that gets **416**, with a `Content-Range: bytes */<length>` header telling the client the real size. ## Download names and headers - `PhysicalFile(path, "video/mp4", fileDownloadName: "consultation.mp4", enableRangeProcessing: true)` adds `Content-Disposition: attachment` with that name, prompting a download instead of inline playback. - Pass a stable `EntityTagHeaderValue` (for example from a content hash) when files can be replaced in place, so `If-Range` compares versions exactly rather than by timestamps rounded to seconds. - Keep authorization on the action: range requests are ordinary requests and each one passes through the pipeline. ## Interview angle Interviewers look for three things: that range processing must be switched on, that `PhysicalFile` differs from `File(string)` in where it looks, and that a byte-array result is not acceptable for large media.

  • A video served with File(stream, "video/mp4", enableRangeProcessing: true) still ignores Range headers. Why?
    Range processing needs the total length. `FileStreamResult` uses `Stream.Length` only when `CanSeek` is `true`; for a non-seekable stream, such as a network stream from storage, the length is unknown and the executor sends the whole body. Use a seekable stream or `PhysicalFile` for local files.
  • What happens when a client resumes with If-Range but the recording was replaced in the meantime?
    The executor compares `If-Range` with the current `Last-Modified` or `ETag`. If they no longer match, it ignores the `Range` header and sends the full new file with 200, so the client does not join bytes from two different files.

  • ASP.NET Core file results honour Range headers by default.
  • PhysicalFile accepts a path relative to the content root.
  • File("recordings/x.mp4", "video/mp4") reads from the app's working directory.
  • Returning File(bytes, ...) is fine for large videos because ranges are supported.
  • A missing file passed to PhysicalFile results in a 404 automatically.