Http Range Requests in ASP.NET Core

Tue Jan 08 2019ProgrammingWeb Development

Here's a small code snippet to use Http Range Requests with ASP.NET Core butchered from this now outdated MSDN article:

using System.IO;
using System.Text;
using Microsoft.AspNetCore.Mvc;

namespace WebApiRangeTest.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class RangeController : ControllerBase
    {
        private static readonly byte[] _content = Encoding.UTF8.GetBytes("abcdefghijklmnopqrstuvwxyz");

        public IActionResult Get()
        {
            MemoryStream stream = new MemoryStream(_content);

            if (stream == null)
            {
                return NotFound();
            }

            return File(stream, "text/plain", enableRangeProcessing: true);
        }
    }
}

The "Request.Headers.Range" method has now been replaced by opting in for this functionality using the enableRangeProcessing boolean when returning a FileStreamResult object.

Have fun!

GatsbyNetlifyReactTypeScriptTailwind CSS
© Copyright 2008-2022 Terry Butler