你好,
我有一个小问题。 我无法让我的服务停止将所有内容转换为 application/json
我试过:
services.AddMvc(config => {config.RespectBrowserAcceptHeader = true;});
我什至尝试使用 [Produces("video/mp4")] 但它说“没有输出格式化程序”
仍然没有结果:
应用程序/json
或者
406 不可接受
如何让它在 .net core 1.0 中工作?
我们将需要有关您尝试执行的操作的更多信息,包括来自您的控制器/操作的一些代码。
如果你从一个 action 方法返回一个非IActionResult
,它会通过输出格式化程序运行以尝试将它写入响应。 默认情况下,我们配置了 JSON 格式化程序。
嗯,还有由 asp.net 核心实现的任何输出格式化程序来处理“视频/mp4”
这是我的控制器
[HttpGet]
[Produces("video/mp4")]
public HttpResponseMessage Get()
{
string videoPath = @"D:\sample.mp4";
if (System.IO.File.Exists(videoPath))
{
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
response.Content = new StreamContent(new FileStream(videoPath, FileMode.Open, FileAccess.Read));
response.Content.Headers.ContentType = new MediaTypeHeaderValue("video/mp4");
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = "sample.mp4"
};
return response;
}
return new HttpResponseMessage(HttpStatusCode.InternalServerError);
}
当我简单地使用 owinservice 时它工作得很好
嗯,还有由 asp.net 核心实现的任何输出格式化程序来处理“视频/mp4”
不。
这是 MVC Core 1.0 中的等效项:
C#
[HttpGet]
public IActionResult Get()
{
string videoPath = @"D:\sample.mp4";
if (System.IO.File.Exists(videoPath))
{
return PhysicalFile(videoPath, "video/mp4", "sample.mp4");
}
return StatusCode(StatusCodes.Status500InternalServerError);
}
完成 :dagger: 谢谢:)
最有用的评论
不。
这是 MVC Core 1.0 中的等效项:
C# [HttpGet] public IActionResult Get() { string videoPath = @"D:\sample.mp4"; if (System.IO.File.Exists(videoPath)) { return PhysicalFile(videoPath, "video/mp4", "sample.mp4"); } return StatusCode(StatusCodes.Status500InternalServerError); }