ASP.NET MVC Not Serving MP3 Files

旧街凉风 提交于 2020-01-05 04:15:07

问题


I am trying to play an mp3 file as background music on a website. To serve it I created a folder called mp3 in Content and added the file to it. The html I use looks like

<audio loop="loop" autoplay="autoplay">
  <source src="/mp3/myfile.mp3" type="audio/mp3" />
</audio>

The browser returns a 404 and yes.. I verified the name of the file is correct and even renamed it to "a.mp3" to make sure nothing funny was happening there.

If I select Open in New Tab I also get 404. (i.e. mydomain.com/mp3/a.mp3 gives a 404)

As an experiment I placed a js file in the mp3 folder.. (mydomain,com/mp3/test.js) and it is served into a new tab with no problem. Also placed a png file in the folder (mydomain.com/mp3/pic.png) and it is served with no problem. Copying it to another folder in use like js or css gives same result. I can serve up the js or css files in the folder but get a 404 if I try to retrieve the mp3 file.

Same results with FF and Chrome so it is not browser specific.

It seems something is specifically causing it to refuse to serve mp3 files.

BTW I also opened the files on my windows desktop and they play with no problem in WMP so there is nothing wrong with the files.


回答1:


First, you should check the path with browser's developer tools (view source) to make sure that the file path is rendered properly. Your problem seem occurred because of wrong file path.

Second, instead of creating src path manually, use @Url.Content() helper with relative virtual path to MP3 file like example below, make sure the correct path is used:

<audio loop="loop" autoplay="autoplay">
  <source src="@Url.Content("~/mp3/myfile.mp3")" type="audio/mp3" />
</audio>

If the page is deployed in production server, make sure that MIME type audio/mp3 is enabled in IIS settings and you have read/write permission for /mp3 folder. You may check (and add) this setting inside <system.webServer> section in either applicationHost.config, machine.config or web.config file:

<staticContent>
     <mimeMap fileExtension=".mp3" mimeType="audio/mp3" />
</staticContent>



回答2:


I finally figured out the problem. I realized that although there was a static file mapping for the MVC Handler, that any requests would still go through the ASP.NET MVC router.

Turns out I had a custom route defined that for security reasons locked down the file types it would serve and mp3 wasn't one of them.



来源:https://stackoverflow.com/questions/52730292/asp-net-mvc-not-serving-mp3-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!