Tag Archives: Routing

Understanding Routing in ASP.NET MVC – PART II

We have already discussed basic about ASP.NET MVC routing here. So in this post we are going to discuss about more on routing with examples.

What is Routing?

Routing is set of rules/pattern matching system that monitor the incoming request from the broswer and set matched location to process further.

The Routing engine uses the virtual route table which helps to find the matching pattern for the incoming request (URL) from the browser.

The route has the following 3 segments:

Controller : define the controller{controller}
Action : define the action{action}
Parameter : define the parameter{id}

Let me explain the above segments with examples.

Example 1: With No paramerter

We can find Route configuration inside App_Start (Folder) –> RouteConfig.cs

Controller:

public ActionResult Index()
{
return View();
}

RouteConfig.cs:

public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute(“{resource}.axd/{*pathInfo}”);
routes.MapRoute(
name: “Default”,
url: “api/{controller}/{action}/{id}”,
defaults: new { id = UrlParameter.Optional });
}
}

We can also state that the default or optional values are provided for all the three segments. The Controller has a default value of “Home”, the action has a default value of “Index” and id is an optional value.

Let us assume www.dotnet-helpers/Home/Index/ has been requested from the browser. Then the default route maps this URL shown below.

controller : Home
action : Index

Example 2: With Parameter

Controller:

public ActionResult Index()
{
return View();
}
public ActionResult Index1(int id)
{
return View();
}

The default route will handle the below requests and redirect to the matched controller and action method ( ie.,  index () and index1 (int id)).

http://localhost:50287/dotnet_helpers/index
http://localhost:50287/dotnet_helpers/index1/1

Let us assume www.dotnet-helpers/Home/Index/500 has been requested from the browser. Then the default route maps URL shown below.

controller : Home
action : Index
id : 3

 

Understanding Routing in ASP.NET MVC – PART 1

Introduction

In this article we are going to discuss about the basic ASP.NET MVC Routing, how it works and difference between ASP.NET web Form request and ASP.NET MVC request.

What is Routing?

Routing is set of rules/pattern matching system that monitors the incoming request from the browser and set matched location to process further.

The Routing engine uses the virtual route table which helps to finding the matching pattern for the incoming request (URL) from the browser.

Request handle in webform and ASP.NET MVC :

In ASP.NET Web forms, the request from the browser is mapped to the page which is physically present in our application/website directory like Home.aspx, AboutUs.aspx.

But in ASP.NET MVC, the request needs to be mapped to a controller and action. In ASP.NET MVC, we don’t have concept of physical file(like aspx).

Difference between In ASP.NET Web Form and  ASP.NET MVC request?

In MVC, we are having the controller and action methods. Each controller has multiple action methods.
Routing takes care of the mapping strategy for mapping request URLs to the controllers and action methods. we know already that .aspx will be available in the ASP.NET Web form request as shown below. But in MVC we dont have any extension like this because it won’t have physical file location (one controller have multiple action).

ASP.NET Web forms request : https://dotnet-helpers.com/MVCRoute/Route.aspx

ASP.NET MVC request : https://dotnet-helpers.com/MVCRoute/Route

MVCRoute is a Controller
Route  is an Action

From above, .aspx is physical file which is present in the application directory so no separate mechanism for handling the request. In ASP.NET MVC, there is no physical file for route, it contains controller and the action.

Flow of Routing :

Routemapping-dotnet-helpers.com1

From above flow, the request is handled by routing. First route engine parses the  incoming URL and  match the request with virtual route table. If request match with the pattern present in the route table then it will redirect to the particular controller and action. If match not found in the route pattern then it will show the 404 error.

Here we have discussed the basics of the routing concept. In upcoming article we will discuss more in depth about routing. Happy coding.