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"];
}
-
1:08:52
Russell Brand
1 hour agoTrump Appoints Elon & Vivek To DOGE As Elites LOSE THEIR MINDS + CNN & MSNBC In FIRE SALE! - SF492
23.4K39 -
LIVE
Nerdrotic
4 hours ago $5.67 earnedUS Congress UFO Hearing LIVE REACTION with Chris Gore | Forbidden Frontier #081
2,819 watching -
LIVE
The Charlie Kirk Show
1 hour agoTear It Down, Build It Up + The Military in Crisis | Huckabee, Prince, Davis | 11.13.24
17,102 watching -
LIVE
Akademiks
1 hour agoKendrick Lamar Announces He Will Headline Superbowl 2025. Clowns Drake w/ sayin 'NO ROUND 2s'
3,066 watching -
LIVE
Misha Petrov
25 minutes agoLeftist LUNATICS Can’t ACCEPT Trump’s Win! Uncovering Their Darkest WARNINGS to Trump Voters
405 watching -
LIVE
GameLeap RAID Shadow Legends
23 minutes agoWE SOLVED SAND DEVIL! Top Tips Every New Player Needs - Raid Shadow Legends
36 watching -
LIVE
Grant Stinchfield
1 hour agoBallot Dumps, More Voters than Registered, Even Deleted Votes,,, It Looks Very Fishy!
899 watching -
1:02:21
The Dan Bongino Show
4 hours agoTrump Makes His BOLDEST Moves Yet (Ep. 2370) - 11/13/2024
543K1.88K -
1:02:59
The Rubin Report
2 hours agoCNN Host Forced to Intervene as Guest Loses His Cool as Republican Calmly States Facts
40.4K44 -
The Boomer Effect
18 hours agoSocial Security - Simple and Smart
14