Streaming Video Content to a Browser using Web API

misc1Recently, a client reached out to me that their video streaming wouldn’t allow for seeking (fast-forward & rewind).  Not knowing a lot about streaming, I did some google searching and found out that if the API is not setup to support seeking, then it will not work.

I found a great starting point for this:

Basically, the browser will request various seek-points in the video and the endpoint must be able to handle this and return the requested chunks of media.  The requests will send a range header value that the API must use to return the proper spot in the video.  Also, the endpoint must set the status code to “Partial Content”.

I refactored the code a bit and created this Streamer class:

    class FileStreamer
        public FileInfo FileInfo { get; set; }
        public long Start { get; set; }
        public long End { get; set; }

        public async Task WriteToStream(Stream outputStream, HttpContent content, TransportContext context)
                var buffer = new byte[65536];
                using (var video = FileInfo.OpenRead())
                    if (End == -1)
                        End = video.Length;
                    var position = Start;
                    var bytesLeft = End - Start + 1;
                    video.Position = Start;
                    while (position <= End)
                        var bytesRead = video.Read(buffer, 0, (int) Math.Min(bytesLeft, buffer.Length));
                        await outputStream.WriteAsync(buffer, 0, bytesRead);
                        position += bytesRead;
                        bytesLeft = End - position + 1;
            catch (Exception ex)
                // fail silently

It takes the file info and the start and end positions, in bytes, to return.

Now, in the controller, we must do a few things:

  1. Set the FileInfo Object on the Streamer object
  2. if the range is set in the header, then we must do the following:
    1. Retrieve the start & end indices from the header
    2. Set the corresponding properties in the Streamer object.
    3.  Set Content Length and the Content Range to the correct values in the response header.
    4. Set the status code to “Partial Content”
  3. if the range is not set, then we must:
    1. Return the media file as normal, which just entails returning the media file
    2. Set the status code to “Ok”

Sample controller code:

        public HttpResponseMessage Get(string filename)

            var filePath = GetFilePath(filename);
            if (!File.Exists(filePath))
                return new HttpResponseMessage(HttpStatusCode.NotFound);

            var response = Request.CreateResponse();

            var streamer = new FileStreamer();
            streamer.FileInfo = new FileInfo(filePath);
            response.Content = new PushStreamContent(streamer.WriteToStream, GetMimeType(filePath));

            RangeHeaderValue rangeHeader = Request.Headers.Range;
            if (rangeHeader != null)
                long totalLength = streamer.FileInfo.Length;
                var range = rangeHeader.Ranges.First();
                streamer.Start = range.From ?? 0;
                streamer.End = range.To ?? totalLength - 1;

                response.Content.Headers.ContentLength = streamer.End - streamer.Start + 1;
                response.Content.Headers.ContentRange = new ContentRangeHeaderValue(streamer.Start, streamer.End,
                response.StatusCode = HttpStatusCode.PartialContent;
                response.StatusCode = HttpStatusCode.OK;

            return response;

the standard way to reference the videos in html is to just use a video tag:

<video style="width:100%;height:100%;"                 


And not your videos support streaming!


  • Greate arcticle! Providing the source code would help a lot.


  • That is a nice article, but it streams from a file on local computer ( localhost) can you try running the code but change the video SRC =
    [video src="" /]

    It does not work anylonger i’m getting this error:

    System.NotSupportedException: ‘This stream does not support seek operations.’


  • Hi,

    Great article. It works perfectly in the .NET framework. I am looking for a similar solution for .net core API. Can you please help with that.



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s