What is equivalent to `MapSpaFallbackRoute` for ASP.NET Core 3.0 endpoints?
Asked Answered
S

4

36

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.

Stereotaxis answered 6/9, 2019 at 12:57 Comment(0)
S
64

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");
    });
}
Stereotaxis answered 6/9, 2019 at 12:57 Comment(2)
Are these alternative lines or both necessary?Apelles
Maybe I can put it this way: as long as MVC has a way to resolve route for HomeController's Index method, which is our fallback, endpoints.MapFallbackToController("Index", "Home"); will work.Stereotaxis
D
4

This decision helped me!

public void Configure(IApplicationBuilder application)
{
    application
        // other extensions...
        .UseEndpoints(endpoints => endpoints.MapDefaultControllerRoute())
        .UseSpa(_ => { }); // extension from 'Microsoft.AspNetCore.SpaServices.Extensions' assembly
}
Davao answered 24/1, 2020 at 12:38 Comment(1)
Please consider adding some explanation and details to your answer.Thoughtout
M
0

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"; }); 
    }
Mcconaghy answered 11/2, 2021 at 23:27 Comment(0)
F
0

In .NET 7 add it to program.cs file as below:

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

https://learn.microsoft.com/en-us/dotnet/core/compatibility/aspnet-core/7.0/fallback-file-endpoints#recommended-action

Fulgurating answered 28/3, 2023 at 20:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.