简体   繁体   中英

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

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 .

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM