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"];
}
-
2:52:40
The Quartering
3 hours agoSaturday Play Through! Clair Obscur: Expedition 33
9.97K1 -
1:19:14
Jeff Ahern
2 hours ago $0.95 earnedThe Saturday Show with Jeff Ahern
53.4K5 -
9:44
Stephen Gardner
23 hours ago🔥Trump gets BIG WIN as Kamala Harris EXPOSED in big scam!!
20.8K117 -
LIVE
GritsGG
3 hours agoWSOW Qualifiers! 👑 2587+ Ws
178 watching -
LIVE
GamerGril
1 hour agoWhen The Pimps In The Crib | Days Gone | New Game +
133 watching -
1:06:57
Winston Marshall
11 hours agoHow The West Enabled The Iran Axis to Grow Unchecked w/ Ambassador Michael Oren
83.6K40 -
LIVE
JdaDelete
13 hours ago $0.04 earnedDuke Nukem 3D | Sega Saturday
88 watching -
LIVE
Behunted
5 hours agoITS HOT HOT WARZONE MAYHEM !! PEW PEW
42 watching -
58:49
Tactical Advisor
4 hours agoUnboxing Tacpack/New 1854 Lever Action | Vault Room Live Stream 029
71.5K7 -
LIVE
Mossy
3 hours ago🔥RumbleCraftSMP🔥sesh time tap in🔥DUCKY IS ON??🔥#RumbleTakeover🔥
28 watching