.NET 6 (Core) Client Side Routing On Page Refresh

On a page refresh or on a direct navigation, the server attempts to handle the request according to the server rules, that is .e.g mapping to a controller action or some static page.

What if we have a URL like this: http://localhost/client/routing/page where the "client/routing/page" should be handled by client side routing, such as react router?

In such a case we need to add a special endpoint to handle unknown path, this keeps the original path allowing the client side routing to handle it


 app.UseEndpoints(endpoints =>
     {
          endpoints.MapControllers();
          endpoints.MapFallbackToFile(@"clientapp\index.html");
     });

Notice: the file path is relative to the wwwroot folder. To change it - use the UseWebRoot method.

Post a Comment

Previous Post Next Post