【问题标题】:How would I implement this dynamic controller action?我将如何实现这个动态控制器动作?
【发布时间】:2021-07-23 18:35:38
【问题描述】:

我正在开发 .NET Core 5 MVC。这可能是一个 xy 问题。

我正在尝试将 .m3u8 文件流式传输到浏览器视频播放器 (video.js)。视频播放器读取.m3u8 并尝试为其视频片段请求.ts 文件,并根据.m3u8 文件按顺序流式传输它们。

我有一个名为Video 的控制器。在这个控制器中,我有一个名为“GetStream”的操作,它返回计算机上的.m3u8 文件。

这是请求:

Request URL: https://localhost:44333/Video/GetStream Request 
Method: GET Status Code: 200  Remote Address: [::1]:44333 Referrer 
Policy: strict-origin-when-cross-origin

回应:

Accept-Ranges: bytes
Content-Length: 172
Content-Type: application/octet-stream
Date: Fri, 23 Jul 2021 18:21:58 GMT
Last-Modified: Fri, 23 Jul 2021 17:50:41 GMT
Server: Microsoft-IIS/10.0
X-Powered-By: ASP.NET

也就是说反应是正确的。问题是当视频播放器尝试从.m3u8 GetStream 文件中的信息中获取.ts 文件时,它会发送此请求:

Request URL: https://localhost:44333/Video/2021-07-23_12-50-350.ts
Request Method: GET
Status Code: 302 
Remote Address: [::1]:44333
Referrer Policy: strict-origin-when-cross-origin

当然,这会返回 404 错误,因为没有任何操作会返回该文件。

================================================ ===============

控制器:

[HttpGet]
public FileResult GetStream()
{
    return PhysicalFile(@"C:...\wwwroot\vods\2021-07-23_12-50-35.m3u8", "application/octet-stream", enableRangeProcessing: true);
}

我是否忽略了什么?

【问题讨论】:

  • 您是否尝试添加操作以返回.ts 文件?
  • @abdusco 你这是什么意思?
  • 类似于GetStream 方法,将.ts 文件名作为路由参数并流式传输实际的.ts 文件
  • [HttpGet("{**tsFilename}")] public ActionResult GetTsFile(string tsFilename)

标签: c# asp.net-mvc .net-core


【解决方案1】:

我必须对动作进行一些路线更改才能将其重定向到正确的位置。

    [HttpGet("[controller]/GetStream")]
    public FileResult GetStream()
    {
        return PhysicalFile(@"C:\..\wwwroot\vods\2021-07-23_13-52-52.m3u8", "application/octet-stream", enableRangeProcessing: true);
    }

    [HttpGet("[controller]/{tsFileName}")]
    public ActionResult GetTsFile(string tsFileName)
    {
        if (string.IsNullOrEmpty(tsFileName))
            return new EmptyResult();

        return PhysicalFile(@"C:\..\wwwroot\vods\" + tsFileName, "application/octet-stream", enableRangeProcessing: true);
    }

感谢@abdusco 为我指明了正确的方向

【讨论】:

    猜你喜欢
    • 2014-08-25
    • 2022-10-19
    • 2011-03-19
    • 2015-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多