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"];
}
-
LIVE
Russell Brand
2 hours agoEstablishment ATTACK RFK Jr As China Unveils Weapons AIMED at America’s Bases! - SF629
3,568 watching -
LIVE
Dr Disrespect
3 hours agoLIVE - DR DISRESPECT - MARVEL RIVALS, PUBG, OFF THE GRID - TRIPLE THREAT GAME CHALLENGE
1,722 watching -
LIVE
Tucker Carlson
1 hour agoBill Gates, Truth About Vaccines, & Big Pharma’s Plot to Destroy Doctors Who Question ”The Science”
5,210 watching -
20:08
Professor Nez
24 minutes ago🚨🔥BRUTAL! Trump ROASTS Newsom SO BAD it BREAKS Gavin for Good
1 -
2:04:38
Side Scrollers Podcast
3 hours agoEveryone HATES Baseball Karen + Gaming’s Newest Virtue Signal + MORE | Side Scrollers Live
9701 -
LIVE
StoneMountain64
1 hour agoDelta Force Budget vs JUICER Loadouts
109 watching -
LIVE
Sean Unpaved
2 hours agoYardline Yarns: Giants Need Juice, Bills-Ravens Classic, Dolphins' Soft Spot & Packers Prove It
144 watching -
1:05:03
Timcast
2 hours agoDemocrat media COVERS UP Irina Zarutska Murder, Second White Woman Killed, Trump Says WAR
79.3K77 -
2:07:35
Steven Crowder
5 hours agoThe Murder of A Ukrainian Refugee is A Tipping Point in American History
277K291 -
1:03:37
The Rubin Report
3 hours agoMedia Caught Trying to Ignore Ugly New Details of Charlotte Train Stabbing Caught on Tape
36K72