Introduction
Asp.net MVC Controllers are responsible for controlling the flow of the application execution. When you make a request (means request a page) to MVC applications, a controller is responsible for returning the response to that request. A controller can have one or more actions. A controller action can return different types of action results to a particular request.
If you notice the below URL, this URL invokes the index action method with in the HomeController class
http://localhost:63861/Home/index
So how these URL’s are mapped to the correct Controller action method and how does the application know to do that? The answer is Asp.net Routing.
Now open the visual studio and create the MVC Demo application, create the HomeControlles class under Controllers folder as shown below
Homecontroller.cs page
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
-
- namespace DemoWebApplication2.Controllers
- {
- public class HomeController : Controller
- {
- public ActionResult Index()
- {
- return View();
- }
-
-
- }
-
- }
Now let’s run the project
Output
If you notice the above output the URL at the moment it doesn’t even have the name of controller and the action method all we have is the server which localhost.don’t have the Controller name and action name but application successfully invoked.
The index function within the home controller so how does the application know to do that? If you look at global.asax and notice that we have this even handler method here Application_start event get execute when the applications start first
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using System.Web.Optimization;
- using System.Web.Routing;
-
- namespace DemoWebApplication2
- {
- public class MvcApplication : System.Web.HttpApplication
- {
- protected void Application_Start()
- {
- AreaRegistration.RegisterAllAreas();
- FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
- RouteConfig.RegisterRoutes(RouteTable.Routes);
- BundleConfig.RegisterBundles(BundleTable.Bundles);
- }
- }
- }
If you notice above code Application_start contains several methods, righ click on Routeconfig method and select Go to Defination then Routeconfig.cs file will open
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using System.Web.Routing;
-
- namespace DemoWebApplication2
- {
- public class RouteConfig
- {
- 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 }
- );
- }
- }
Routeconfig.cs file located in App_start folder under Project folders
If you clearly observe the Routeconfig.cs file code has some parameter
- name :It says “Default” meaning this is default route
- url: It contains "{controller}/{action}/{id}" meaning is url pattern contains controller/actionmethod/id
ex: http://localhost:63861/Home/index in the above example Home is the controller name and Index is the action method name and id is optional
- defaults:It contains “new { controller = "Home", action = "Index", id=UrlParameter.Optional }” here Controller is Home and action is Index and Id is optional parameter http://localhost:63861
Inthe above URL it dosen’t have any controller and index methods but applications invoked successfully without any errors because in Routeconfig file we have url parmeter if we doesn’t specify any controller name and index method then in default parmerter treated Home is the default controller and index is the default action method and id is optional
url: "{controller}/{action}/{id}"
defaults: new { controller = "Home", action = "Index", id=UrlParameter.Optional }
Now pass the controller name and index action in url and see the output
Output is same because in Routeconfig file we have default controller and action methods now change the index action method name to Index1 and run the application and see the output
- public string Index1()
- {
- return "Hi Friends how r u";
- }
Output
it showing 404 error because in default parameters there is no action with Index1 name
- defaults: new { controller = "Home", action = "Index", id=UrlParameter.Optional }
now revert back the changes change the action name Index1 to Index and run the application and see the output.
- public string Index()
- {
- return "Hi Friends how r u";
- }
It’s executed successfully and displayed the output
Now will discuss about how to pass the id in URL , as shown below we pass the parameters
Ex
http://localhost:63861/Home/index/15
In the above example 15 parameter and will retrieve the parameter in following way
- public string Index(string id)
- {
- return "id : " + id; ;
- }
Output
Conclusion
In this blog we have learned what is controller and how controller will work and how to pass the parameters in URL.