6

I've got a very simple angular app project that needs to do nothing more than serve static files from wwwroot. Here is my Startup.cs:

public class Startup
{
    public void ConfigureServices(IServiceCollection services) { }

    public void Configure(IApplicationBuilder app)
    {
        app.UseIISPlatformHandler();
        app.UseStaticFiles();
    }

    // Entry point for the application.
    public static void Main(string[] args) => WebApplication.Run<Startup>(args);
}

Whenever I launch the project with IIS Express or web I always have to navigate to /index.html. How do I make it so that I can just visit the root (/) and still get index.html?

2 Answers 2

7

You want to server default files and static files:

public void Configure(IApplicationBuilder application)
{
    ...
    // Enable serving of static files from the wwwroot folder.
    application.UseStaticFiles();
    // Serve the default file, if present.
    application.UseDefaultFiles();
    ...
}

Alternatively, you can use the UseFileServer method which does the same thing using a single line, rather than two.

public void Configure(IApplicationBuilder application)
{
    ...
    application.UseFileServer();
    ...
}

See the documentation for more information.

Sign up to request clarification or add additional context in comments.

2 Comments

You must do it in this order (app.UseDefaultFiles before app.UseStaticFiles). Otherwise use app.UseFileServer, which is just shorthand for the same thing.
@ChrisHalcrow Thanks, now fixed. For future reference, you can actually just edit other peoples answers directly.
6

Simply change app.UseStaticFiles(); to app.UseFileServer();

public class Startup
{
    public void ConfigureServices(IServiceCollection services) { }

    public void Configure(IApplicationBuilder app)
    {
        app.UseIISPlatformHandler();
        app.UseFileServer();
    }

    // Entry point for the application.
    public static void Main(string[] args) => WebApplication.Run<Startup>(args);
}

Comments

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.