
Hi Folks! Are you having trouble playing videos from the App_Data directory served by your controller? In this post I am going to demonstrate several solutions for video streaming in ASP.NET MVC. But the most complete one, the final solution (Solution4controller) in the source-code takes the cake, allowing the user to rewind and forward the video as they please. So, let's enumerate the solutions:
Please, clone the repository here.
- Solution 1: works, but it's limited. Simply returning a FilePathResult (video playback doesn't rewind or can't go forward);
- Solution 2: works, but it's also limited. Simply returning a FileStreamResult (video playback doesn't rewind or can't go forward);
- Solution 3: works, but it's also limited. Uses an action method called WriteContentToStream to stream the video back to the client (video playback doesn't rewind or can't go forward);
- Solution 4: works perfectly, but it uses unsafe code, so in order to take advantage of it you'll have to change the settings in your project and allow unsafe code (don't forget to set up for both Debug and Release configurations).

But how Solution 4 works? Go to the Solution4Controller.cs in the solution. The answer is the StaticFileHandler.cs in the framework. I was wondering why the video would play just fine if the file resided outside the App_Data directory, applying the path directly in the src attribute of a video html element, but as long as I put the files inside App_Data and tried to serve from a controller, the playback would be very limited.

So basically to make everything work, I'd have to adapt the functions and methods responsible for delivering files from the StaticFileHandler static class. I must say it's a really thought-out class, even allowing for path overrides in the code. It's a good example of the use of advanced features of the language and even reflection (because not everything was public and could be easily accessed).
Copy everything from the Solution4Controller.cs, make small modifications to it and be happy. And voilà, enjoy this piece of work (provided by me, of course). And thanks to Microsoft for allowing developers to examine the .NET Framework source-code.