ASP.NET Core 3.1 - Failed to load resource: the server responded with a status of 404 ()

╄→尐↘猪︶ㄣ 提交于 2021-01-29 22:46:40

问题


I'm trying to load a resource file from the /wwwroot/3d_models folder location.

In the Startup.cs I enabled app.UseStaticFiles(); and added the following snippet:

app.UseStaticFiles(new StaticFileOptions()
{
    FileProvider = new PhysicalFileProvider(Path.Combine(env.WebRootPath, @"3d_models")),
    RequestPath = new AspNetCore.Http.PathString("/models")
});

app.UseDirectoryBrowser(new DirectoryBrowserOptions()
{
    FileProvider = new PhysicalFileProvider(Path.Combine(env.WebRootPath, @"3d_models")),
    RequestPath = new AspNetCore.Http.PathString("/models")
});

Under the /wwwroot folder (in solution explorer), I added the resource file:

Yet still, I'm getting this 404 error. Interestingly, when I try to go to the file directly from the browser, I also get the following error:

What am I doing wrong here?

Update

I found the answer in Static files in ASP.NET Core.


回答1:


The folder is called 3d_models whereas the URL is models/xbox.glt.

Shouldn't the folder name match where it's looking for it? (or vice versa)

Only parameterless UseStaticFiles will look for assets from webroot. If you pass parameters then by nature it will look for these assets outside the webroot.

That's what appears to be happening in your case as they are looking for a models folder outside the webroot.

NOTE:

Here's a good link explaining Parameter-ed vs Parameter-less call of UseStaticFiles().

Static files in ASP.NET Core




回答2:


I found the answer here. Basically, I had to set the mimetype/mapping for the .glb file type.

See Static files in ASP.NET Core.



来源:https://stackoverflow.com/questions/63081107/asp-net-core-3-1-failed-to-load-resource-the-server-responded-with-a-status-o

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