We've seen how we can specify default values for route parameters in the template, but there's also another way: by overloading the MapControllerRoute extension method that takes an object containing default values. Instead of supplying these defaults as strings, you can have this:
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller}/{action}/{id?}",
defaults: new { controller = "Home", action = "Index" });
});
This is valid even if you don't have the tokens in the route, as follows:
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "My/Route",
defaults: new { controller = "My", action = "Route" });
});
Remember that you do have to supply controller and action; if they are not present in the template...