app.UseDefaultFiles in Kestrel not doing anything?
Asked Answered
B

2

9

I have this in a Startup.cs file for a small project for a webserver hosting static files with Kestrel:

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<MvcOptions>(options =>
        {
            options.Filters.Add(new RequireHttpsAttribute());
        });
    }
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        var configBuilder = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("config.json");
        var config = configBuilder.Build();

        var options = new RewriteOptions()
           .AddRedirectToHttps();

        app.UseRewriter(options);

        DefaultFilesOptions defoptions = new DefaultFilesOptions();
        defoptions.DefaultFileNames.Clear();
        defoptions.DefaultFileNames.Add("index.html");
        app.UseDefaultFiles(defoptions);


        app.UseStaticFiles();
        app.UseStaticFiles(new StaticFileOptions()
        {
            FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), @"static")),
            RequestPath = new PathString("")
        });

        loggerFactory.AddConsole(config.GetSection(key: "Logging"));
    }
}

However, it doesn't try to load an index.html or anything. If I access it manually, it does indeed work.

Any ideas?

Thanks

Berryberryhill answered 17/11, 2017 at 13:25 Comment(1)
What is the path to your index.html file - is it under wwwroot?Epitome
E
14

Ideally your index.html should be under the web root path (wwwroot), however, if your file is under ContentRootPath\static (as it appears to be), you will need to change your code to specify the DefaultFilesOptions.FileProvider as follows:

PhysicalFileProvider fileProvider = new PhysicalFileProvider(
    Path.Combine(Directory.GetCurrentDirectory(), @"static"));
DefaultFilesOptions defoptions = new DefaultFilesOptions();
defoptions.DefaultFileNames.Clear();
defoptions.FileProvider = fileProvider;
defoptions.DefaultFileNames.Add("index.html");
app.UseDefaultFiles(defoptions);

app.UseStaticFiles();
app.UseStaticFiles(new StaticFileOptions()
{
    FileProvider = fileProvider,
    RequestPath = new PathString("")
});

Note: you are probably better off using IHostingEnvironment.ContentRootPath (env.ContentRootPath) rather than Directory.GetCurrentDirectory() on the first line (just in case you want to change the content root in your WebHostBuilder at some point).

Epitome answered 17/11, 2017 at 13:44 Comment(2)
Thanks a lot, works perfectly. Didn't figure out I had to use FileProvider with DefaultFilesOptions as well (-_-)Berryberryhill
This is, of course, straight out of the documentation. And using either this example, or the documentation example, I cannot get this to work on a WebAPI Core 2.1 project.Agglutination
F
4

Kudos to CalC for his insight, which got me closer to the answer for asp.net core 2.2. Here's what I ended up needing to do to serve a default document outside of the main web server root in Azure:

public void Configure(IApplicationBuilder app){...

// content is physically stored at /site/abc on an Azure Web App, not /site/wwwroot/abc
string path = Path.Combine(Directory.GetCurrentDirectory(), "abc");
PhysicalFileProvider fileProvider = new PhysicalFileProvider(path); 

DefaultFilesOptions options = new DefaultFilesOptions 
    {
        FileProvider = fileProvider, 
        RequestPath = "/ABC",
    }; 
options.DefaultFileNames.Add("index.html");
app.UseDefaultFiles(options);

app.UseStaticFiles(new StaticFileOptions
                   {
                       FileProvider = fileProvider,
                       RequestPath = "/ABC"
                   });
}

The key was using matching RequestPath and FileProvider values

Faustofaustus answered 26/6, 2019 at 2:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.