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"];
}
-
28:30
Degenerate Jay
13 hours agoThe Rejected 007 Quantum Of Solace Ending That Would Have Changed James Bond
6692 -
11:37
ARFCOM Reviews
12 hours ago $0.01 earnedNew Budget Comp'd Carry || Canik MC9 PRIME
1.36K3 -
8:22
Rethinking the Dollar
15 hours agoSilver vs CBDCs: The Battle for Financial Freedom
1.55K -
55:15
MTNTOUGH Fitness Lab
16 hours agoTJ Dillashaw: Embracing Fear as a Privilege | MTNPOD #112
1.88K1 -
12:24
T-SPLY
21 hours agoKamala’s COMEBACK FLOPS as Dems DITCH Her!
1.85K5 -
47:42
Steph & Kayls
20 hours agoReflecting On How Being Spicy Creators Changed Our Relationship, Life And Bank Account | Ep. 2
1.91K4 -
7:03
DropItLikeItsScott
21 hours agoFosTech Drone Predator Pistol Suppressed
2.04K -
3:31:01
FreshandFit
10 hours agoAfter Hours w/ Dan Cates
105K135 -
1:37:17
Badlands Media
10 hours agoBaseless Conspiracies Ep. 128: The Accelerationist Cults Grooming Kids into Terrorism with BX
137K45 -
2:44:29
TimcastIRL
12 hours agoBail DENIED For Leftist Who FIREBOMED Democrat Governor's Mansion, Mangione Effect | Timcast IRL
267K104