35
votes

In ASP.NET Core 2.x I used standard routes registation Configure method of Startup class to register fallback route for SPA application using MapSpaFallbackRoute extension method from Microsoft.AspNetCore.SpaServices.Extensions Nuget package:

public void Configure(IApplicationBuilder app)
{
    // ...
    app.UseMvc(routes =>
    {
        routes.MapRoute(
            name: "default",
            template: "{controller=Home}/{action=Index}/{id?}");
        routes.MapSpaFallbackRoute(
            name: "spa-fallback",
            defaults: new { controller = "Home", action = "Index" });
    });
}

I cannot find similar extension method when using ASP.NET Core 3.0 recommended UseEndpoints extension method for endpoints registration.

3

3 Answers

59
votes

In ASP.NET Core 3.0 extension method MapFallbackToController has same functionality to MapSpaFallbackRoute extension method.

public void Configure(IApplicationBuilder app)
{
    app.UseRouting();
    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllerRoute(
            name: "default",
            pattern: "{controller=Home}/{action=Index}/{id?}");

        endpoints.MapFallbackToController("Index", "Home");
    });
}
3
votes

This decision helped me!

public void Configure(IApplicationBuilder application)
{
    application
        // other extensions...
        .UseEndpoints(endpoints => endpoints.MapDefaultControllerRoute())
        .UseSpa(_ => { }); // extension from 'Microsoft.AspNetCore.SpaServices.Extensions' assembly
}
0
votes

This helped me out when upgrading from 2.1 to 3.1 aspnetcore3.1

        app.UseRouting();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");

            endpoints.MapControllerRoute(
                name: "api",
                pattern: "api/{controller=Default}/{action=Index}/{id?}"
                );

            endpoints.MapFallbackToController("Index", "Home");
        }).UseSpa(_ => { _.Options.DefaultPage = "spa-fallback"; }); 
    }