Premium Only Content

Part 4 Controllers in an mvc application
In this video we will discuss about controllers. Please watch Part 3 of MVC tutorial before proceeding. In Part 3, we discussed that, the URL - http://localhost/MVCDemo/Home/Index will invoke Index() function of HomeController class. So, the question is, where is this mapping defined. The mapping is defined in Global.asax. Notice that in Global.asax we have RegisterRoutes() method.
RouteConfig.RegisterRoutes(RouteTable.Routes);
Right click on this method, and select "Go to Definition". Notice the implementation of RegisterRoutes() method in RouteConfig class. This method has got a default route.
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 }
);
}
The following URL does not have id. This is not a problem because id is optional in the default route.
http://localhost/MVCDemo/Home/Index
Now pass id in the URL as shown below and press enter. Nothing happens.
http://localhost/MVCDemo/Home/Index/10
Change the Index() function in HomeController as shown below.
public string Index(string id)
{
return "The value of Id = " + id;
}
Enter the following URL and press enter. We get the output as expected.
http://localhost/MVCDemo/Home/Index/10
In the following URL, 10 is the value for id parameter and we also have a query string "name".
http://localhost/MVCDemo/home/index/10?name=Pragim
Change the Index() function in HomeController as shown below, to read both the parameter values.
public string Index(string id, string name)
{
return "The value of Id = " + id + " and Name = " + name;
}
Just like web forms, you can also use "Request.QueryString"
public string Index(string id)
{
return "The value of Id = " + id + " and Name = " + Request.QueryString["name"];
}
-
5:41:58
MattMorseTV
11 hours ago $112.25 earned🔴Antifa RIOT vs. Federal OFFICERS.🔴
188K219 -
1:05:28
Man in America
13 hours agoLIVE: Digital ID & the DEATH of Freedom—An URGENT Warning
64.3K74 -
40:24
The Connect: With Johnny Mitchell
1 day ago $6.52 earnedInside The WORST Drug-Infested Slums Of Medellin, Colombia
22.9K9 -
1:43:56
Tundra Tactical
10 hours ago $34.11 earned🛑LIVE NOW!! FBI Gets Caught LYING About Good Guys With Guns For 10 YEARS!!!!
53.2K4 -
2:12:01
BlackDiamondGunsandGear
2 days agoAFTER HOURS ARMORY / Antifa / Lies/ Prison time
31.5K1 -
2:12:00
DLDAfterDark
9 hours ago $13.62 earnedThe After Hours Armory! Tonight is The Chat's Chat! God, Guns, and Gear!
42.6K4 -
3:32:18
Mally_Mouse
12 hours ago🌶️ 🥵Spicy BITE Saturday!! 🥵🌶️- Let's Play: Phasmophobia
52K5 -
1:13:19
iCkEdMeL
8 hours ago $10.16 earnedChaos Explodes in Chicago & Portland | Feds Clash with Protesters!
52.4K29 -
21:54
Exploring With Nug
1 day ago $10.53 earnedScuba Diving Missing Person Search Leads to Discovery of Classic Cars!
66.8K19 -
8:06:32
Phyxicx
13 hours agoStar Wars: Movie Battles II Community Event hosted by ReaperAF95 - 10/4/2025
31.5K