hi
i was reading about MapControllerRoute() what i understansed that i has two main processes
- defining the Route Templates and there crosponding pattern
- add thos route template to the routes table (that the UseRouting() use when matching incomming request with the route template in the route table);
- Run the UseEndponints() that excutes the suitable action according to the matched url
what i want to ask is the part of the defining the route template and there crospnding pattern, the app includes only one patten that are defined throught the MapControllerRoute() like below code , then i have 3 controller action in my Homecontroller class then haw many template will be added to the table
C#
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");

Jignesh KumarRank #91 · 22.9K reputationPosted Sep 23, 2024, 4:05 AM EDT
Hello Mina,
If you want to understand route table with debugging, you can add the below line in your code and debug,
MVC:
protected void Application_Start() { AreaRegistration.RegisterAllAreas(); RouteConfig.RegisterRoutes(RouteTable.Routes); // Total Count the number of routes int routeCount = RouteTable.Routes.Count; // Loop through all created route foreach(var rout in RouteTable.Routes) { onsole.WriteLine($"route: {rout}"); } }MVC Core :
app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); // Count and log the number of endpoints (routes) var routeCount = endpoints.DataSources .SelectMany(ds => ds.Endpoints) .Count(); // Loop through all created route foreach(var rout in endpoints.DataSources .SelectMany(ds => ds.Endpoints).ToList()) { onsole.WriteLine($"route: {rout}"); } });Jignesh KumarRank #91 · 22.9K reputationPosted Sep 22, 2024, 5:36 AM EDT
Hello Mina,
Let's take an example for your routing,
app.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}");Create Home controller with Index method and Details method which take id as integer parameter,
using Microsoft.AspNetCore.Mvc; namespace YourNamespace.Controllers { public class HomeController : Controller { // This method will handle requests to "/Home/Index" public IActionResult Index() { // Return a view or any other action result return View(); } // This method will handle requests like "/Home/Details/5" public IActionResult Details(int? id) { if (id == null) { return NotFound("ID is required"); } // Custom logic as per your requirement return View(id); } } }Sam HobbsRank #107 · 20.5K reputationPosted Sep 22, 2024, 4:23 PM EDT
The
MapControllerRoutedocumentation, as in:ControllerEndpointRouteBuilderExtensions.MapControllerRoute Method (Microsoft.AspNetCore.Builder) | Microsoft Learn
https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.builder.controllerendpointroutebuilderextensions.mapcontrollerroute?view=aspnetcore-8.0
I think is misleading. The documentation says that it Adds endpoints (plural). Yet the following:
Routing to controller actions in ASP.NET Core
https://learn.microsoft.com/en-us/aspnet/core/mvc/controllers/routing?view=aspnetcore-8.0
Says
MapControllerRouteis used to create a single route.Does (not dose) that help?
mina shaker157 reputationPosted Sep 22, 2024, 3:56 PM EDT
@Jignesh Kumar i do understand this but what i dont understand how many route will be added to the route table
mina shaker157 reputationPosted Sep 22, 2024, 4:56 PM EDT
@Sam Hobbs then which one is correct , that why i dont like to read from the docs is all misleading and it is not for begginers