I want to provide my Angular app through the root route /
and the REST API through /api/*
. For the Angular routing I have to redirect all requests to /index.html except the requests for existing files (e.g. media files) and my API controller routes.
With the Startup.cs
below it's close to be working:
- Opening http://localhost:5000 will return the index.html and the angular routing is working, that I get redirected to http://localhost:5000/home
- API calls are still working
- Existing CSS/JavaScript files are returned
The following is not working: Refreshing or opening directly http://localhost:5000/home will end up in a 404. I guess /home
is not redirected to the index.html.
What I'm missing here?
public class Startup
{
private readonly IWebHostEnvironment _webHostEnvironment;
public Startup(IWebHostEnvironment webHostEnvironment)
{
_webHostEnvironment = webHostEnvironment;
}
public void ConfigureServices(IServiceCollection services)
{
services.AddSpaStaticFiles(options =>
{
options.RootPath = "wwwroot";
});
services.AddControllers();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseDefaultFiles();
app.UseSpaStaticFiles();
app.UseCors();
app.UseSwagger();
app.UseSwaggerUI(c => { /*...*/ });
app.UseRouting();
app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
}
}