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
BonginoReport
2 hours agoMore Questions Than Answers After Assassination Attempt Anniversary - Hayley Caronia (Ep.89)
18,634 watching -
UPCOMING
Donald Trump Jr.
1 hour agoHow Butler Changed Our Nation Forever, Plus My Remarks at Turning Point | TRIGGERED Ep.258
9.83K8 -
LIVE
The Jimmy Dore Show
1 hour agoTrump Gets RATIO’D Over Epstein Post! Israel Building Gaza Concentration Camp! w/ Adam Carolla
7,362 watching -
Dr Disrespect
8 hours ago🔴LIVE - DR DISRESPECT - PUBG - DANGEROUS
92.9K13 -
1:12:52
Kim Iversen
3 hours agoTucker Calls Out Israeli Blackmail — GOP Reels Into Civil War
70.4K68 -
57:19
Candace Show Podcast
3 hours agoTrump Gone Wild! Is Jeffrey Epstein Even Dead? | Candace Ep 215
78.4K197 -
58:16
Redacted News
5 hours agoThe Secret Space Program IS REAL and this Whistleblower is EXPOSING all of it
32.5K158 -
7:52:45
Viss
8 hours ago🔴LIVE - The 5 Wins PUBG Challenge! Viss w/ Dr Disrespect
12.6K -
1:35:04
vivafrei
4 hours agoTrump Satisfied with Answers on Butler? Epstein Saga Continues! Douglass Mackey Special Guest!
151K43 -
40:59
Kimberly Guilfoyle
3 hours agoBorder Security is National Security, Live with Tyler O'Neil & Ammon Blair | Ep237
39.4K14