Keywords | asp.net mvc routing tutorial, asp.net mvc routing basics, asp net mvc routing best practices, asp.net mvc routing examples
- Must read Custom Paging in MVC using jQuery
ASP.Net MVC Routing Tutorial
Routing is one of the fundamental concepts for ASP.NET MVC web applications. In this asp.net MVC blog post I will explain asp.net MVC routing with examples.
ASP.NET MVC Routing is a process of mapping a URL (Uniform Resource Locator) request to a specific controller action. In simple words: ASP.net MVC routing simplifies your MVC web application URL structure like http://localhost:54604/customer/product
It means routing helps us to create user-friendly URLs and those URLs you can map with your technical controller names and action names.
ASP.Net MVC Routing Examples
With the asp net mvc routing best practices behind us, we can now examine some routing examples. Find the asp net mvc routing configuration below with URL parameters:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
using System.Web.Mvc; using System.Web.Routing; namespace MVC_Routing { public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { // routes -> collection routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); // to prevent unauthorized resources access routes.IgnoreRoute("{resource}.config"); routes.MapRoute( name: "CustomerKey1", url: "Customer", defaults: new { controller = "Customer", action = "GoToProduct", id = UrlParameter.Optional } ); routes.MapRoute( name: "CustomerKey2", url: "Customer/Customer", defaults: new { controller = "Customer", action = "GoToProduct", id = UrlParameter.Optional } ); routes.MapRoute( name: "CustomerKey3", url: "", defaults: new { controller = "Customer", action = "GoToProduct", id = UrlParameter.Optional } ); //Note: Don't remove it. routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Customer", action = "Index", id = UrlParameter.Optional } ); } } } |
If we talk about the benefit of using asp.net MVC routing, It helps you to achieve greater search engine optimization with asp.net MVC routing SEO.
Note: Always remember route name should be unique across the entire application. Route name can’t be duplicate.