Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serve static file index.html by default

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?

like image 213
kspearrin Avatar asked Nov 19 '15 03:11

kspearrin


People also ask

Which of the below folder serves static files by default?

Static files are stored within the project's web root directory. The default directory is {content root}/wwwroot , but it can be changed with the UseWebRoot method.

Are HTML files static files?

Static files, such as HTML, CSS, images, and JavaScript, are assets an ASP.NET Core app serves directly to clients by default.


2 Answers

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);
}
like image 39
kspearrin Avatar answered Nov 15 '22 10:11

kspearrin


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.

like image 105
Muhammad Rehan Saeed Avatar answered Nov 15 '22 11:11

Muhammad Rehan Saeed