is the UseRouting() or the UseEndpoints() resposible for selecting the controller , as far as i know the the latest is resposible for invoking the action after the match is done using the first
Loading
is the UseRouting() or the UseEndpoints() resposible for selecting the controller , as far as i know the the latest is resposible for invoking the action after the match is done using the first
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Amit MohantyRank #112 · 20K reputationPosted Nov 19, 2024, 12:24 AM EST
UseRouting():This function includes routing middleware in pipeline.
Use Endpoints(): End the routing cycle by invoking the matched endpoint: for example an action of a controller.
In summary, the UseRouting() selects the endpoint; for example, it may be a controller and an action depending on routed data. UseEndpoints() will run the endpoint-on that execute the controller action or handler.
Jayraj ChhayaRank #304 · 8.2K reputationPosted Nov 19, 2024, 12:47 AM EST
Hi @mina shaker,
In ASP.NET Core, the middleware responsible for selecting the controller is
UseRouting(). This middleware analyzes the incoming request and matches it to the appropriate route defined in your application. It sets up the routing context, which includes determining the controller and action to be invoked based on the URL.Once the routing is complete,
UseEndpoints()comes into play. This middleware is responsible for executing the matched endpoint, which includes invoking the action method of the selected controller. In summary,UseRouting()is tasked with selecting the controller, whileUseEndpoints()handles the invocation of the action after the match is established.Here’s a simple example of how these middlewares are typically configured in the
Startup.csfile:public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { app.UseRouting(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); }This configuration ensures that the routing and endpoint execution processes are correctly set up in your ASP.NET Core application.
Jignesh KumarRank #91 · 22.9K reputationPosted Nov 19, 2024, 12:51 AM EST
Hello Mina,
You need to configure these methods in the
Startupclass of your project using the code below:public class Startup { public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { app.UseRouting(); // Responsible for matching routes and selecting controllers app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); }); } }Benjamin Gabriel10 reputationPosted Nov 19, 2024, 2:37 AM EST
UseRouting()is responsible for matching the incoming request to a route and identifying the appropriate controller and action based on the route table, but it does not execute the action.UseEndpoints()comes after and is responsible for executing the matched endpoint, such as invoking the action method in the controller. Together,UseRouting()sets up the match, andUseEndpoints()completes the request processing by executing it.