Controller In ASP.NET MVC 5

Let us learn all about Controllers with examples in ASP.NET MVC 5, via this article.
 
 
In this article, you will learn the following points about Controllers in MVC 5.
  • What is Controller in ASP.NET MVC 5?
  • How to create an ASP.NET MVC 5 application using Visual Studio 2017
  • How to create a Controller in MVC 5?
  • How to call a Controller
  • How to change the default controller
  • How the default controller gets executed
  • The flow of Controller in ASP.NET MVC 5
In the previous article, we got an introduction to ASP.NET MVC. If you are new, visit the following link.
Prerequisites

Introduction

 
MVC stands for Model, View, and Controller. MVC separates the application into three components like Model, View, and Controller. We will learn Controller in detail. A controller is one of the important components of MVC.
 

What is Controller in ASP.NET MVC 5?

  • A controller can contain an action and non-action method.
  • It is used to handle the user request coming from the browser.
  • It will check the request from the browser and identify the action method and return the respective view.
  • A controller is inherited from “ControllerBase” class which is inside the “System.Web.MVC” namespace.
  • While creating controller remember it always suffix as “Controller” keyword.
  • Default Controller is “HomeController” and “Index” is view.
Following are the steps to create ASP.NET MVC 5 application using Visual Studio 2017,
 
Step 1
 
Open => Visual Studio 2017 and go to File >> New >> Click on Project as follow.
 
 
Step 2
 
Select “ASP.NET Web Application” and provide a meaningful name like “MVCControllerDemo” and Click on “Ok” button.
 
 
Step 3
 
Select the “Empty” template and check the “MVC” checkbox from “New Web Application” window as follow.
 
 
Step 4
 
The default project structure will create as follows,
 
 

How to create a controller in MVC 5?

 
The following steps will help you to create a controller in ASP.NET MVC 5.
 
Step 1
 
Go to solution explorer Right-click on “Controller” Folder >> Click on Add >> click on “Controller” as follow.
 
 
Step 2
 
Select “MVC 5 Empty Controller” from the window and click on “Add” button.
 
 
Step 3
 
Provide a meaningful name like “HomeController” and Click on "Add" button.
 
 
Sample code of Controller as follows,
  1. using System.Web.Mvc;  
  2.   
  3. namespace MVCControllerDemo.Controllers  
  4. {  
  5.     public class HomeController : Controller  
  6.     {  
  7.         // GET: Home  
  8.         public ActionResult Index()  
  9.         {  
  10.             return View();  
  11.         }  
  12.     }  
  13. }  
How to check if the Controller is inherited from “ControllerBase” class which is inside the “System.Web.Mvc” namespace?
 
Right-click on "Controller" click on “Go to definition” as follow.
 
 

How to create a controller with read/write action in MVC 5 using the default template

 
Step to create a controller with read/write action in MVC 5 as follow,
 
Step 1
 
Go to solution explorer Right-click on “Controller” Folder >> Click on Add >> click on “Controller” as follow.
 
 
Step 2
 
Select "MVC 5 Controller with read/write actions" from the window and click on “Add” button.
 
 
Step 3
 
Provide a meaningful name like “ProductController” and Click on “Add” button as follow.
 
 
Default code is generated for "MVC 5 Controller with read/write actions" as follow,
  1. using System.Web.Mvc;  
  2.   
  3. namespace MVCControllerDemo.Controllers  
  4. {  
  5.     public class ProductController : Controller  
  6.     {  
  7.         // GET: Product  
  8.         public ActionResult Index()  
  9.         {  
  10.             return View();  
  11.         }  
  12.   
  13.         // GET: Product/Details/5  
  14.         public ActionResult Details(int id)  
  15.         {  
  16.             return View();  
  17.         }  
  18.   
  19.         // GET: Product/Create  
  20.         public ActionResult Create()  
  21.         {  
  22.             return View();  
  23.         }  
  24.   
  25.         // POST: Product/Create  
  26.         [HttpPost]  
  27.         public ActionResult Create(FormCollection collection)  
  28.         {  
  29.             try  
  30.             {  
  31.                 // TODO: Add insert logic here  
  32.   
  33.                 return RedirectToAction("Index");  
  34.             }  
  35.             catch  
  36.             {  
  37.                 return View();  
  38.             }  
  39.         }  
  40.   
  41.         // GET: Product/Edit/5  
  42.         public ActionResult Edit(int id)  
  43.         {  
  44.             return View();  
  45.         }  
  46.   
  47.         // POST: Product/Edit/5  
  48.         [HttpPost]  
  49.         public ActionResult Edit(int id, FormCollection collection)  
  50.         {  
  51.             try  
  52.             {  
  53.                 // TODO: Add update logic here  
  54.   
  55.                 return RedirectToAction("Index");  
  56.             }  
  57.             catch  
  58.             {  
  59.                 return View();  
  60.             }  
  61.         }  
  62.   
  63.         // GET: Product/Delete/5  
  64.         public ActionResult Delete(int id)  
  65.         {  
  66.             return View();  
  67.         }  
  68.   
  69.         // POST: Product/Delete/5  
  70.         [HttpPost]  
  71.         public ActionResult Delete(int id, FormCollection collection)  
  72.         {  
  73.             try  
  74.             {  
  75.                 // TODO: Add delete logic here  
  76.   
  77.                 return RedirectToAction("Index");  
  78.             }  
  79.             catch  
  80.             {  
  81.                 return View();  
  82.             }  
  83.         }  
  84.     }  
  85. }  

How to call controller in MVC 5?

 
Open any browser and enter the URL like “DomainName/ControllerName” as follows.
 

How to change the default controller?

 
Open the Routeconfig.cs file default file as follows.
  1. public class RouteConfig  
  2.     {  
  3.         public static void RegisterRoutes(RouteCollection routes)  
  4.         {  
  5.             routes.IgnoreRoute("{resource}.axd/{*pathInfo}");  
  6.   
  7.             routes.MapRoute(  
  8.                 name: "Default",  
  9.                 url: "{controller}/{action}/{id}",  
  10.                 defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }  
  11.             );  
  12.         }  
  13.     }  
Change the controller name as "Product" controller instead of the “Home” controller inside the Routeconfig file as follow.
 
 

How does the default controller get executed

 
The default controller gets executed based on configuration setting which applied in RouteConfig.cs file. We have provided the “Product” controller as default controller in Routeconfig.cs file. After executing the application default “ProductController” get executed.
 

The flow of Controller in ASP.NET MVC

 
Following are diagrams which will help you to understand the flow of ASP.NET MVC Controller.
As per the above figure, the user enters the URL on the browser, the given request goes to the server and calls the routing, which will execute the appropriate controller. And based on the request the controller executes the appropriate controller action method. It will pass the request to the model if the model has a database related operation then it will perform some database-related operation and get back the result to the controller. After it's completed, this controller returns the response to the user.
 
References
Interview questions for freshers on the controller,
  • What is the controller in ASP.NET MVC/MVC 5?
  • What is the base class of Controller?
  • How to change the default Controller in ASP.NET MVC / MVC 5?
  • How does the default controller get executed in ASP.NET MVC 5?
I hope you understand the concepts of the controller in asp.net MVC 5.
 
Thanks for reading.


Similar Articles