The concept of Routing is very vast and I have covered it in 6 tutorials which are:
So make sure you cover each of these one by one.
app.Use(async (context, next) => { var controller = context.Request.RouteValues["controller"]; await next(); });In this scenario I get null value for the controller variable, and this is shown by breakpoint value as illustrated in the below image:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { app.UseStaticFiles(); app.Use(async (context, next) => { var controller = context.Request.RouteValues["controller"]; await next(); }); app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); }); }
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { app.UseStaticFiles(); app.UseRouting(); app.Use(async (context, next) => { var controller = context.Request.RouteValues["controller"]; await next(); }); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); }); }Run the application and check the value of ‘controller’ through the breakpoint. This time you will get the value of the controller in the variable. This is all due to Endpoint routing. Check the below image that shows the breakpoint’s value:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { //Define this before other middleware that needs routing access app.UseRouting(); // Here add middlewares that needs routing access app.UseEndpoints(endpoints => { //Define endpoint routes here endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); }); }
Endpoint routing’s two extension methods are:
app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); });
app.UseEndpoints(endpoints => { endpoints.MapRazorPages(); });
app.UseEndpoints(endpoints => { endpoints.MapGet("/", context => context.Response.WriteAsync("Hello world")); });
Similarly, we can configure other Http verb route like MapPost, MapDelete, MapPut, etc.
Endpoint routing makes the ASP.NET Core framework more flexible since it decouples the route matching and resolution functionality from the endpoint dispatching functionality, which until now was all bundled in with the MVC middleware.
Let’s take a few examples to understand them:
app.UseEndpoints(endpoints => { endpoints.MapGet("/someurl", context => context.Response.WriteAsync("Hello world")).RequireCors("policy-name"); });
app.UseEndpoints(endpoints => { endpoints.MapGet("/secured", context => context.Response.WriteAsync("Secured Page")) .RequireAuthorization(new AuthorizeAttribute() { Roles = "admin" }); });
You already know that when there are more than one routes defined in an application, then the routes are applied in the order in which they are defined. You can now override this thing as you have prior information about a lot of route things.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { //Define this before other middleware that needs routing access app.UseRouting(); // Redirect code app.Use(async (context, next) => { var controller = context.Request.RouteValues["controller"]; var action = context.Request.RouteValues["action"]; if ((controller.ToString() == "Business") && (action.ToString() == "Deal")) { context.Response.Redirect("/contact"); } await next(); }); app.UseEndpoints(endpoints => { //Define endpoint routes here endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); }); }You can download the full source code of this tutorial: Download
app.UseEndpoints(endpoints => { // add the old routes endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); });However if you want to keep working with your old routes then you can disable the Endpoint routing from the ConfigureServices() method:
public void ConfigureServices(IServiceCollection services) { services.AddMvc(option => option.EnableEndpointRouting = false); // disable endpoint routing }
Endpoint routing allows ASP.NET Core applications to determine the endpoint that will be dispatched, early on in the middleware pipeline, and so makes the framework more flexible.
I hope you liked this tutorial, so please share this on your facebook and twitter accounts, and let the knowledge flows
Great article! Not just this, but all articles here are fabulous.
My question is with endpoint routing, if I provide defaults like:
endpoints.MapControllerRoute(
name: “default”,
pattern: “{controller=Admin}/{action=Index}/{id?}”);
Why does my browser not show locahost:2131/admin/index, it only shows localhost:2131
i searched the internet but haven’t found an answer just as yet. Pls revert!
Hello,
Your code is looks correct, may be some issue with browser caching. Try incognito mode of the browser and see if it works.
Thank you.