file-uploadasp.net-core-mvchtml5-videorelative-addressing

Video not playing in ASP.NET Core MVC


I created a project with ASP.NET Core 6 MVC web app. In this project, videos are uploaded. To prevent all users from accessing the videos, a folder named "File" was created outside the wwwroot and the videos were uploaded to the File folder. Videos will not play when referenced from the File folder with the code below. What is the reason for this problem and what should I do to solve it?

<video src="../File/Videos/VideoName.mp4" controls width="640" height="300" loop/>

Solution

  • When you want to Serve files outside of web root, You need to configure the Static File Middleware as follows

    //.....other middleware...
    
    app.UseStaticFiles(new StaticFileOptions
    {
        FileProvider = new PhysicalFileProvider(
               Path.Combine(builder.Environment.ContentRootPath, "File")),
        RequestPath = "/File"
    });
    
    //.....other middleware...
    

    Then Use this code to show video:

    <video src="@Url.Content("File/video/VideoDemo.mp4")" controls width="640" height="300" loop/> 
    

    Demo:

    enter image description here

    enter image description here

    More details you can refer to this Docs