问题
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