Routing In ASP.NET And ASP.NET MVC With Example

What is Routing

URL Routing or URL Rewriting is a technique using that we can define user-friendly URL which is useful for Search Engine Optimization (SEO).

Using ASP.NET routing or ASP.NET MVC Routing we can achieve that easily.

Rather than calling a physical File name like 'Home/about.aspx' we can use 'Home/about', User cannot remember files extension like .aspx or .php so we can use Routing and make URL easy to remember for them.

Routing in ASP.NET

Let's create ASP.NET Routing first.
Step 1: Create a new Web Application Project and add 2 pages like Default.aspx and About.aspx.

Step 2: Add Global Application class 'Global.asax'.

Step 3: Add reference System.Web.Routing in your project.
Step 4: Open global.asax file and add the following code in 'Application_Start'
  1. void Application_Start(object sender, EventArgs e)   
  2. {  
  3.     // Code that runs on application startup  
  5.     System.Web.Routing.RouteTable.Routes.MapPageRoute("Home","Home/Index","~/Default.aspx");  
  7.     System.Web.Routing.RouteTable.Routes.MapPageRoute("About","Home/About-WDI","~/About.aspx");  
  9. }  
Here we are going to use RouteTable for Routing url, RouteTable.Routes.MapPageRoute method ask for 3 parameter, that is 'routeName', 'routeUrl' and 'physicalFile' rather than calling Default.aspx, user can call Home/Index.
Now run the application and enter url like 'localhost:/Home/Index' it will automatically invoke Default.aspx page.

Same way call Home/About-WDI as in the following screenshot,


Routing in ASP.NET MVC

Routing in MVC is easy, here we have Route.Config.cs file in App_Start Folder, You can define Routes in that file,
By default route is: Home controller - Index Method. 
  1. // default Route  
  2. routes.MapRoute(  
  3.       name: "Default",  
  4.       url: "{controller}/{action}/{id}",  
  5.       defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }  
routes.MapRoute has attributes like name, url and defaults like controller name, action and id (optional).
Now let us create one MVC Application and open RouteConfig.cs file and add the following custom route. 
  1. // Custom Route  
  2. routes.MapRoute(  
  3.   name: "about",  
  4.   url: "Home/About-WDI",  
  5.   defaults: new { controller = "Home", action = "About", id = UrlParameter.Optional }  
  6. );  
It will invoke 'Home' controller 'About' Action Method.
So for example if user hits url : 'localhost:/Home/About-WDI' it will invoke Home controller 'About' Action Method.

Parameter Id is optional here, it can be used while working with id Field like Home/editProduct/3

Enable Attributes Routing

You can also Enable Attributes routing by writing the following code in Route.config.cs file.
  1. //Enabling Attribute Routing   
  2.            routes.MapMvcAttributeRoutes();  
Now rather than writing and managing Routes from Route.config you can manage routes from Controller. As in the following screenshot we have added [Route("Home/ContactUs")] above Contact() ActionResult.
  1. [Route("Home/ContactUs")]  
  2. public ActionResult Contact()  
  3. {  
  4.     ViewBag.Message = "Your contact page.";  
  6.     return View();  
  7. }  
Now run your application and enter url like: 'localhost:/Home/contactUs' invokes Contact Method.
Hope you like this article. Same way if you are working with areas then you can manage the routes from AreaRegistration.cs file.

Please download code files for more details.