9

I currently have a generated index.html, js and other static files living in a folder and I'm marking that folder as a static folder (by adding the following in the Configure method in Startup.cs:

   app.UseDefaultFiles();
   app.UseStaticFiles(new StaticFileOptions()
    {
        FileProvider = new Path.Combine(env.ContentRootPath, @"../build")),
        RequestPath = new PathString("/app/")
    });

Is there a way to set index.html as the default response for this */app route? Because right now localhost:5000/app/ returns a 404 while localhost:5000/app/index.html returns the index.html.

EDIT: I missed to mention that I did try using app.UseDefaultFiles() like mentioned in docs but it does not work for me. The server still returns a 404

1

3 Answers 3

11

One of the comments in that docs has clarified it:

Kieren_Johnstone Featured May 22, 2017

The section, "Serving a default document" misses some vital information. If you configure your UseStaticFiles to work off a non-root RequestPath, you need to pass the same FileProvider and RequestPath into both UseDefaultFiles and UseStaticFiles, it seems. You can't always just call it as stated in the section.

So that means, you should write something like this to enable your specified folder to provide a default page:

    app.UseDefaultFiles(new DefaultFilesOptions()
    {
        FileProvider = Path.Combine(env.ContentRootPath, @"../build")),
        RequestPath = new PathString("/app/")
    });
    app.UseStaticFiles(new StaticFileOptions()
    {
        FileProvider = Path.Combine(env.ContentRootPath, @"../build")),
        RequestPath = new PathString("/app/")
    });
Sign up to request clarification or add additional context in comments.

1 Comment

This is still the case for .NET Core 5.0, in case someone runs into it.
2

Use this:

public void Configure(IApplicationBuilder app)
{
    // Serve my app-specific default file, if present.
    DefaultFilesOptions options = new DefaultFilesOptions();
    options.DefaultFileNames.Clear();
    options.DefaultFileNames.Add("mydefault.html");
    app.UseDefaultFiles(options);
    app.UseStaticFiles();
}

For more details follow this link:

https://learn.microsoft.com/en-us/aspnet/core/fundamentals/static-files
and go to section: "Serving a default document"

1 Comment

he has index.html, UseDefaultFiles() includes it by default, no need to add mydefault.html
1

from documentation:

Setting a default home page gives site visitors a place to start when visiting your site. In order for your Web app to serve a default page without the user having to fully qualify the URI, call the UseDefaultFiles extension method from Startup.Configure as follows.

public void Configure(IApplicationBuilder app)
{
    app.UseDefaultFiles();
    app.UseStaticFiles(); // For the wwwroot folder

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

UseDefaultFiles must be called before UseStaticFiles to serve the default file.

2 Comments

Thanks for getting back. I tried this, but still returns a 404. Does this work if a different static folder (other than wwwroot) is used?
@ArvindVenkataraman, where is you app folder located physically?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.