When developing WebAPI servers using ASP.NET Core, it's common to deploy an Angular front-end within the wwwroot
folder. Often, the Angular app handles routing separately, leading to issues when users refresh the page.
Since ASP.NET Core processes HTTP requests before Angularโs routing takes effect, the server may not recognize client-side routes. As a result, refreshing a page within the Angular app results in a 404 Not Found
error.
To resolve this, we can implement middleware that redirects unknown routes back to the root path (/
), allowing Angular to handle routing properly.
app.Use(async (context, next) =>
{
await next();
if (context.Response.StatusCode == 404)
{
context.Request.Path = "/";
await next();
}
});
ยฉ 2025 juniyunapapa@gmail.com.