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:55:33
MattMorseTV
7 hours ago $93.87 earned🔴Portland ANTIFA vs. ICE.🔴
146K295 -
3:13:00
Badlands Media
21 hours agoThe Narrative Ep. 40: Acceleratia.
51.6K13 -
LIVE
SpartakusLIVE
6 hours ago#1 Solo Spartan Sunday || TOXIC Comms, TACTICAL Wins, ENDLESS Content
909 watching -
49:45
Sarah Westall
5 hours agoComedians take Center Stage as World goes Nuts w/ Jimmy Dore
31.3K17 -
3:26:14
IsaiahLCarter
13 hours ago $5.88 earnedAntifa Gets WRECKED. || APOSTATE RADIO 030 (Guests: Joel W. Berry, Josie the Redheaded Libertarian)
43.2K1 -
LIVE
CassaiyanGaming
4 hours agoArena Breakout: Infinite Dawg
210 watching -
2:24:32
vivafrei
14 hours agoEp. 284: Ostrich Crisis Continues! Kirk Updates! Fed-Surrection Confirmed? Comey Indicted! AND MORE!
131K188 -
LIVE
Cewpins
4 hours agoSunday Sesh!🔥Rumble Giveaway Tonight!🍃420💨!MJ !giveaway
128 watching -
3:03:11
Conductor_Jackson
6 hours agoLet’s Play BioShock Infinite Burial at Sea Episode 2!
24.6K -
5:21:16
EricJohnPizzaArtist
6 days agoAwesome Sauce PIZZA ART LIVE Ep. #63: Charlie Sheen
54.7K4