It is very easy to play or display video in ASP.NET MVC using HTML5. We can do this using custom action filter in MVC. We can play various files like MP4, Ogg, WebM, etc. format.
Here are step by step tutorial to play video in ASP.NET MVC using HTML5.
Here is example for this.
In this example we are taking .MP4 file to play in browser.
STEP : 1
Create an ASP.NET MVC Empty application in visual studio. Give project name like ‘PlayVideoInMVC’.
STEP : 2
After creating empty application create new folder ‘VideoDataFile’ after that add video files in this folder.
STEP : 3
Now we are going to add custom action filter. Add new folder ‘CustomDataResult’ and add new class file ‘VideoDataResult.cs’. In this class we need to inherit ‘ActionResult’ class and override ‘ExecuteResult’ method.
In method we write code like read video file and send response back to controller using “context.HttpContext.Response.BinaryWrite” method.
Below is code for ‘VideoDataResult.cs’ file :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Web; using System.Web.Hosting; using System.Web.Mvc; namespace PlayVideoInMVC.CustomDataResult { public class VideoDataResult : ActionResult { /// <summary> /// The below method will respond with the Video file /// </summary> /// <param name="context"></param> public override void ExecuteResult(ControllerContext context) { var strVideoFilePath = HostingEnvironment.MapPath("~/VideoFiles/Test2.mp4"); context.HttpContext.Response.AddHeader("Content-Disposition", "attachment; filename=Test2.mp4"); var objFile = new FileInfo(strVideoFilePath); var stream = objFile.OpenRead(); var objBytes = new byte[stream.Length]; stream.Read(objBytes, 0, (int)objFile.Length); context.HttpContext.Response.BinaryWrite(objBytes); } } } |
STEP 4 :
Now we are going to add our controller that can take care of Video data result. Now add one MVC controller and name it ‘VideoDataController’ in ‘Controller’ folder.
Below is code for ‘VideoDataController’ file :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
using System; using System.Collections.Generic; using System.Linq; using System.Web; using PlayVideoInMVC.CustomDataResult; using System.Web.Mvc; namespace PlayVideoInMVC.Controllers { public class VideoDataController : Controller { // // GET: /VideoData/ public ActionResult Index() { return new VideoDataResult(); } } } |
STEP : 5
Now add new controller to view or display video name it ‘ViewVideoController’, after that add ‘view’ and in this ‘view’ add HTML5 video tag to play video in video player.
In this view we also add one download video link. This link allows to download video. On clicking of this link you will see the download file dialog box.
Below is the HTML markup of ‘View’ :
1 2 3 4 5 6 7 8 9 |
@{ ViewBag.Title = "Index"; } <h2>Play Video</h2> <h3><a href="@Url.Action("Index", "VideoData")">Download Video</a> </h3> <video width="320" height="240" controls autoplay="autoplay"> <source src="@Url.Action("Index", "VideoData")" type="video/mp4"> </video> |
We need to set ‘src’ property of video tag to get video data and play that. Now run the application with this URL ‘http://localhost:[your port]/ViewVideo’. I ran this in chrome. You can run on any other HTML5 enabled browser.
Here is a very useful link to check that does your browser support HTML5 video and also which video format support.
Important : One important thing to note here is that we can use our custom action filter for two purposes, first to display video on browser and Second allow to download video file.
So the same controller serves two purposes. We can centralize our code by doing this.
Output
Best ASP.NET MVC Hosting Recommendation
ASPHostPortal.com
ASPHostPortal.com’s Best ASP.NET hosting platform on Windows 2012 and Windows 2008 is compatible with ASP.NET MVC hosting. Needless to say, you’ll be able to really feel very comfy with their hosting service. Their best and inexpensive ASP.NET MVC hosting package is starting from $1.00/mo only. And with their promo code, you will get free domain as well. Not just that, in addition they supply 30 days cash back guarantee. Their servers are 99.99% uptime, it’s important believe which you require for your ASP.NET MVC site, so your website is by no means down.
HostForLIFE.eu
HostForLIFE.eu offer the Best, Cheap and Recommended ASP.NET MVC hosting start from €1.29/month. They can load your site against anticipated traffic and recommend the best option for you. HostForLIFE.eu is Microsoft No #1 Recommended Windows and ASP.NET Hosting in European Continent. Their service is ranked the highest top #1 spot in several European countries, such as: Germany, Italy, Netherlands, France, Belgium, United Kingdom, Sweden, Finland, Switzerland and many top European countries.
DiscountService.com.au
Get high functionality, best uptime along with the most dependable ASP.NET MVC Hosting site with them. ASP.NET MVC hosting from DiscountService.com.au provides a protected, trustworthy and performance-driven foundation for your website and apps. If you’re searching for the correct Windows ASP.NET hosting that support ASP.NET MVC hosting provider, they are the correct selection for you. They have proactive monitoring down to seconds with reactive options in spot to ensure the stability in the services they offer. All hosting servers are monitored 24/7/365. They use enterprise software to monitor their whole network infrastructure. Their very best and inexpensive ASP.NET MVC hosting price begins from $2.00 monthly.