Serve static file index.html by default


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:


wwwroot


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?


/index.html


/


index.html




2 Answers
2



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.


UseFileServer


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



See the documentation for more information.





You must do it in this order (app.UseDefaultFiles before app.UseStaticFiles). Otherwise use app.UseFileServer, which is just shorthand for the same thing.
– Chris Halcrow
Jun 27 at 22:11


app.UseDefaultFiles


app.UseStaticFiles


app.UseFileServer





@ChrisHalcrow Thanks, now fixed. For future reference, you can actually just edit other peoples answers directly.
– Muhammad Rehan Saeed
2 days ago



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


app.UseStaticFiles();


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);
}






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

how to run turtle graphics in Colaboratory

Export result set on Dbeaver to CSV