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.
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");
});
}
This decision helped me!
public void Configure(IApplicationBuilder application)
{
application
// other extensions...
.UseEndpoints(endpoints => endpoints.MapDefaultControllerRoute())
.UseSpa(_ => { }); // extension from 'Microsoft.AspNetCore.SpaServices.Extensions' assembly
}
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"; });
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.