Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 371
Nenad Avatar asked Sep 06 '19 12:09

Nenad


People also ask

What is MapSpaFallbackRoute?

MapSpaFallbackRoute(IRouteBuilder, String, Object, Object, Object) Configures a route that is automatically bypassed if the requested URL appears to be for a static file (e.g., if it has a filename extension).

What is endpoint in ASP.NET Core?

An Endpoint is an object that contains everything that you need to execute the incoming Request. The Endpoint object contains the following information. Metadata of the request. The delegate (Request handler) that ASP.NET core uses to process the request.

Which ASP.NET Core no longer depends on the system?

Fast: ASP.NET Core no longer depends on System. Web. dll for browser-server communication. ASP.NET Core allows us to include packages that we need for our application.


2 Answers

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");     }); } 
like image 161
Nenad Avatar answered Oct 12 '22 01:10

Nenad


This decision helped me!

public void Configure(IApplicationBuilder application)
{
    application
        // other extensions...
        .UseEndpoints(endpoints => endpoints.MapDefaultControllerRoute())
        .UseSpa(_ => { }); // extension from 'Microsoft.AspNetCore.SpaServices.Extensions' assembly
}
like image 24
Stanislav Seregin Avatar answered Oct 11 '22 23:10

Stanislav Seregin