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?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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.
Method 2
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);
}
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0