In this tutorial we will learn the basics of asp.net mvc routing. After reading this, you will be able to understand how routing maps request URL to a specific controller action.

ASP.NET MVC Routing is a process of mapping a URL (Uniform Resource Locator) request to a specific controller action. The functionality of the routing process is implemented in System.Web.Routing. Routing plays a vital role in the execution flow of ASP.NET MVC application.

If you don’t know how to create a simple ASP.NET MVC Web Application using empty project, then just see the previous tutorial.

Let’s see how routing works in ASP.NET MVC Application.

Global.asax is a file where you can define your application route under Application_Start function. This is global.asax file code from the ASP.NET MVC application which we have created in the previous tutorial.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

using System.Web.Routing; namespace FirstMVCApplication

{

public class MvcApplication : System.Web.HttpApplication

{

protected void Application_Start()

{

AreaRegistration.RegisterAllAreas();

RouteConfig.RegisterRoutes(RouteTable.Routes);

}

}

}

In Application_Start method, Routes are configured with application start with the second line of code by calling a RegisterRoutes in RoutConfig.cs class file.

We can find RouteConfig.cs class under App_Start folder in ASP.NET MVC application which contains RegisterRoutes method. Here is the implementation of RouteConfig class.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

using System.Web.Routing; namespace FirstMVCApplication

{

public class RouteConfig

{

public static void RegisterRoutes(RouteCollection routes)

{

routes.IgnoreRoute(“{resource}.axd/{*pathInfo}”); routes.MapRoute(

name: “Default”,

url: “{controller}/{action}/{id}”,

defaults: new { controller = “Home”, action = “Index”, id = UrlParameter.Optional } );

}

}

}

Only one route is defind under RegisterRoutes, which is default route.

name: “Default” => Name of the Route.

=> Name of the Route. url: “{controller}/{action}/{id}” => It represents that URL will be first Controller, then Action and then Id(Optional)

Note:- When you see URL arrive in the form of {X}/{Y}/{Z}, then the first part is Controller name, second part is Action name and the thirs part is an Id parameter.

=> It represents that URL will be first Controller, then Action and then Id(Optional) When you see URL arrive in the form of {X}/{Y}/{Z}, then the first part is Controller name, second part is Action name and the thirs part is an Id parameter. defaults: new { controller = “Home”, action = “Index”, id = UrlParameter.Optional } => Our default controller will be Home, default action will be Index, and Id will be optional.

Let’s try to run your application.

When you will run your application, it will show output page and you will see that visual studio has directed the browser to port 50248 (http://localhost:50248/). You will see a different port number because visual studio allocates a random port number when the project is created.

The routing engine maps the URL to the corresponding controller as:

It looks the controller segment in URL. If this segment is empty then it takes the default controller (E.g. Home).

Then it looks Action segment in URL. If this segment is empty then it takes the default Action(E.g. Index).

Id will be ignored because it is optional and not provided.

It means you can request any of the following URLs, and they will be directed to the index action on the home controller.

http://localhost:50248/

http://localhost:50248/Home

http://localhost:50248/Home/Index

Custom ASP.NET MVC Routing

If you want to add your own route, you can do it easily. Just follow the following steps.

Consider we have a page that contains the product of customers. Following is the code which will route to the customer page.

routes.MapRoute(

“Customer”,

“Customer/{action}/{id}”,

defaults: new { controller = “Customer”, action = “Product”, id = UrlParameter.Optional }

);

Step 1 : Add the above code in RegisterRoutes method under RouteConfig.cs class.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

using System.Web.Routing; namespace FirstMVCApplication

{

public class RouteConfig

{

public static void RegisterRoutes(RouteCollection routes)

{

routes.IgnoreRoute(“{resource}.axd/{*pathInfo}”); routes.MapRoute(

“Customer”,

“Customer/{action}/{id}”,

defaults: new { controller = “Customer”, action = “Product”, id = UrlParameter.Optional }

); routes.MapRoute(

name: “Default”,

url: “{controller}/{action}/{id}”,

defaults: new { controller = “Home”, action = “Index”, id = UrlParameter.Optional } );

}

}

}

Step 2 : Run your application with the URL “http://localhost:50248/customer/product ” . You will get a 404 error. It is because the routing engine will see CustomerController, which is not available in your project.

Step 3 : Add CustomerController in your project :

Right click on Controller folder in solution explorer -> Choose Add -> Controller

Step 4 : Choose MVC 5 Controller – Empty option and then click ‘Add’ button.

Step 5 : Enter the Controller name (CustomerController) and then click ‘Add’ button.

Now a new CusomterController.cs class file will open.

Step 6 : Now replace the Index method with this following code.

public string Product()

{

return “This is Customer’s product page”;

}

Step 7 : Run your application. Again you will see the result page from the default route. But when you enter this specific URL “http://localhost:50248/customer/product” then you will see customer’s product page.

Share this: Facebook

LinkedIn

Reddit

Google

Pinterest

Twitter

Tumblr

Email

More

Telegram

WhatsApp



Skype

Pocket



Print

