aspnet / StaticFiles

[Archived] Middleware for handling requests for file system resources including files and directories. Project moved to https://github.com/aspnet/AspNetCore
Apache License 2.0
114 stars 72 forks source link

Strange 404 when combining UseDefaultFiles and UseStaticFiles #214

Closed space-alien closed 6 years ago

space-alien commented 6 years ago

I want to serve static files from a different folder than wwwroot. Let's pretend this folder is named statics.

Let's say my default document is index.html.

For some reason, the following code results in a 404 if the default document is requested... UNLESS I place a redundant index.html file in the wwwroot folder in my project. (This file must have the same name as the one I want to serve from my statics folder.)

Once that's done, the index.html file in my statics folder is served as desired. It's as if the server is checking for file existence in wwwroot in spite of the StaticFileOptions.

app.UseDefaultFiles();

// I want to serve static files from a folder, not wwwroot:
app.UseStaticFiles(new StaticFileOptions()
{
    FileProvider = new PhysicalFileProvider(
        Path.Combine(Directory.GetCurrentDirectory(), @"statics")),
    RequestPath = new PathString("") // This line is not required.
});
Tratcher commented 6 years ago

UseDefaultFiles and UseStaticFiles need to use the same FileProvider so they're both looking in the same place. UseFileSystem is a good shortcut for this, or you can change the default in IHostingEnvironment.WebRootFileProvider.

space-alien commented 6 years ago

Thank you, that makes sense.

I think you might have meant UseFileServer in your comment above.

Tratcher commented 6 years ago

Yes, UseFileServer