15 49.0138 8.38624 1 0 4000 1 https://codeisall.com 300
Routing in .NET MVC

Routing in MVC


In the previous article, you have learned about how to map the database data to the model and display it in a view with Dapper Micro ORM in the MVC application.

In this article, you will learn about routing in MVC, We will see how an incoming browser request routes in an MVC Project.

This article will be helpful to beginners and students who are learning MVC. 

What is ASP.NET MVC Routing?

Routing is a pattern matching system that is used for mapping an incoming browser request to a specific controller and actions.

When a MVC application launches then the application registers one or more patterns with the route table of the framework , and tell the route engine to what to when a browser request comes that match with the route pattern of the application.

The work of the routing engine is to check that if the request that comes from the browser matches with the framework pattern or not, according to the result of the request, the routing engine passes that request forward.

Request mapping.

Let’s consider that you have launched the application and the routing patterns are defined by the routing engine,

  • Request: Browser Request sent that comes to the routing engine,
  • Response: If the Request pattern matches the defined request pattern in the routing engine then it redirects to the appropriate method. If the Request pattern does not match with the pattern defined in the routing engine then it returns an error 404 not found.

Routing patterns in an MVC Application

when we create a new MVC application, the application is already configured to use routing.

When an MVC application first run, the Application_Start() method is called. This method, in turn, calls the RegisterRoutes() method. The RegisterRoutes() method creates the route table.

If you see in the App_Start folder there will be RouteConfig.cs file in the application, there will be one method called RegisterRoutes.

In this method the route of the application is being defined, via the method MapRoute method of RouteCollection class.

In that method there are three parameters like name,url,defaults. Consider the below example for example.

Name : Route Name (“Default”)

URL : url of the request with parameters (e.g. – “{controller}/{action}/{id}”)

Defaults : Method that calls when the application run in the browser (e.g. new { controller = “Home”, action = “Index”, id = UrlParameter.Optional }).

So, from the above example you can see that when application will run the first page want to display(E.g. – SignIn, Landing page) that URL should be set as defaults in the RouteConfig.cs file.

Example of Routing in an MVC Application.

Suppose you have a controller named Home.cs and this controller contains method as below.

Here is how you can call the method and the difference between URL will be cleared to you.

  1. https://localhost:44310/Home/Index OR https://localhost:44310/Home
  2. https://localhost:44310/Home/Add
  3. https://localhost:44310/Home/Edit?Id=32

Because of the way that the MVC framework invokes controller actions, the URL /Home matches the Index() method so in the 1st method there are two URLs that can be called this method.

In the 3rd method there Id parameter passing so that will be added after the method name with the question mark in front of the parameter name. If there are multiple parameters are passing then it will be like https://localhost:44310/Home/Edit?Id=23&Age=23&Count=1. So after the first parameter, every parameter will be passed by adding with the & sign as given in the example.

I hope this article will help you to understand what is routing and how it works in an MVC application. we will be happy to help you if you have any issues or queries. Comment your suggestions and issues and we will try to resolve it.

How to use Dapper ORM in C#
Dependency Injection in C#