我想在页面加载时从AppDomain.BaseDirectory中显示随机电影。
RandomVideo.cshtml
<video width="320" height="240" controls>
<source src="MyMovie1.mp4" type="video/mp4"> */ MyMovie1 should not be hard coded*/
</video>
答案 0 :(得分:2)
我将如何做到这一点:
@model VideoModel
<video width="320" height="240" controls>
<source src="@Model.VideoSource" type="video/mp4">
</video>
public class VideoModel
{
public string VideoSource { get; set; }
}
public class VideoController : Controller
{
public ActionResult PlayVideo()
{
var model = new VideoModel();
var videos = Directory
.GetFiles(AppDomain.CurrentDomain.BaseDirectory)
.Where(f => f.EndsWith(".mp4"))
.ToList();
var randomVideo = videos[new Random().Next(0, videos.Count)];
model.VideoSource = randomVideo;
return View(model);
}
}