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"];
}
-
DVR
Geeks + Gamers
2 hours agoSonic 3 DESTROYS Mufasa And Disney, Naughty Dog Actress SLAMS Gamers Over Intergalactic
5.65K1 -
51:59
The Dan Bongino Show
3 hours agoDemocrat Donor Admits The Scary Truth (Ep. 2393) - 12/23/2024
399K727 -
2:32:15
Matt Kohrs
14 hours agoRumble CEO Chris Pavlovski Talks $775M Tether Partnership || The MK Show
74.9K17 -
28:23
Dave Portnoy
14 hours agoDavey Day Trader Presented by Kraken - December 23, 2024
90.4K31 -
59:29
BonginoReport
5 hours agoTrump, Murder Plots, and the Christmas Miracle: Evita + Jack Posobiec (Ep.110) - 12/23/2024
87.5K66 -
2:59:14
Wendy Bell Radio
7 hours agoNothing To See Here
82.9K45 -
2:12:18
TheDozenPodcast
23 hours agoIslam vs Christianity: Bob of Speakers' Corner
84.9K16 -
14:36
The StoneZONE with Roger Stone
1 day agoRoger Stone Delivers Riveting Speech at Turning Point’s AMFEST 2024 | FULL SPEECH
94.4K25 -
18:59
Fit'n Fire
13 hours ago $6.41 earnedZenith ZF5 The Best MP5 Clone available
50.8K2 -
58:34
Rethinking the Dollar
22 hours agoTrump Faces 'Big Mess' Ahead | RTD News Update
40.8K5