Controllers In MVC Application

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
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6.   
  7. namespace DemoWebApplication2.Controllers  
  8. {  
  9.     public class HomeController : Controller  
  10.     {  
  11.         public ActionResult Index()  
  12.         {  
  13.             return View();  
  14.         }  
  15.   
  16.           
  17.     }  
  18.   
  19. }  
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

 

Global.asax.cs file

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using System.Web.Optimization;  
  7. using System.Web.Routing;  
  8.   
  9. namespace DemoWebApplication2  
  10. {  
  11.     public class MvcApplication : System.Web.HttpApplication  
  12.     {  
  13.         protected void Application_Start()  
  14.         {  
  15.             AreaRegistration.RegisterAllAreas();  
  16.             FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);  
  17.             RouteConfig.RegisterRoutes(RouteTable.Routes);  
  18.             BundleConfig.RegisterBundles(BundleTable.Bundles);  
  19.         }  
  20.     }  
  21. }  

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

 

Routeconfig.cs file

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using System.Web.Routing;  
  7.   
  8. namespace DemoWebApplication2  
  9. {  
  10.     public class RouteConfig  
  11.     {  
  12.         public static void RegisterRoutes(RouteCollection routes)  
  13.         {  
  14.             routes.IgnoreRoute("{resource}.axd/{*pathInfo}");  
  15.   
  16.             routes.MapRoute(  
  17.                 name"Default",  
  18.                 url"{controller}/{action}/{id}",  
  19.                 defaultsnew { controller = "Home", action = "Index", id = UrlParameter.Optional }  
  20.             );  
  21.         }  
  22.     }  

 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
  1. public string Index1()  
  2. {  
  3.    return "Hi Friends how r u";  

Output

 

it showing 404 error because in default parameters there is no action with Index1 name 

  1. 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.

  1. public string Index()  
  2. {  
  3.    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

  1. public string Index(string id)  
  2. {  
  3.    return "id : " + id; ;  
  4. }  

 

Output


Conclusion

In this blog we have learned what is controller and how controller will work and how to pass the parameters in URL.

Ebook Download
View all
Learn
View all