Controllers In ASP.NET MVC Applications

Introduction

 
This article will explain the concept of Controllers in ASP.NET MVC. 

What is a Controller in ASP.NET MVC applications?

 
A controller in ASP.NET MVC is a class having a set of public methods. These public methods of the controller are called action methods or simply, actions. The action methods in MVC application handle the incoming HTTP Requests.
 
The Controllers in ASP.NET MVC application logically group similar type of actions together. This grouping of actions together allows us to define sets of rules such as routing, caching, and authorization which is going to be applied collectively.
 
Controller is the component which is going to receive the incoming HTTP Request and then process that request. While processing the request, the controller does several things. It works with the model. Then, it selects a view by passing the model object which renders the user interface from the model data. The View then generates the necessary HTML and then the controller sends HTML back to the client who initially made the request. So, we can consider that the Controller is the heart of an MVC application.
 
By convention, in ASP.NET MVC, the controller classes should reside in the project's root level Controllers folder and should inherit from the System.Web.Mvc.Controller base class.
 
Let’s understand the Controller in ASP.NET MVC application with an example.
 
Step 1
 
Open Visual Studio 2015 or a version of your choice and create a project.
 
Step 2
 
Choose a "web application" project and give an appropriate name to your project.
 
Controllers In ASP.NET MVC Application
 
Step 3
 
Select the "empty" template, check the MVC checkbox, and click OK.
 
Controllers In ASP.NET MVC Application
 
Step 4
 
Right-click on Controllers folder and add a new controller.
 
Controllers In ASP.NET MVC Application
 
A window will appear. Choose MVC5 Controller-Empty and click "Add".
 
Controllers In ASP.NET MVC Application
 
After clicking on "Add", another window will appear with DefaultController. Change the name to HomeController and click "Add". The HomeController will be added under the Controllers folder. Don’t change the Controller suffix for all controllers, change only the highlight, and instead of Default, just change Home.
 
Controllers In ASP.NET MVC Application
 
At this point, if you run the application, then you will get the following error.
 
Controllers In ASP.NET MVC Application
 

Fixing the Error

 
In order to fix the above error, we need to add a view with the name “Index”. We will discuss views in detail in our next article. So here, we will fix the above issue with another way. The following is the function that is auto-generated by the HomeController class.
 
To fix the issue, modify the return type of the Index() action method from “ActionResult” to “string”. Then, return a string, as shown below.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6.    
  7. namespace MvcController_Demo.Controllers  
  8. {  
  9.     public class HomeController : Controller  
  10.     {  
  11.         // GET: Home  
  12.         public string Index()  
  13.         {  
  14.             return "Hello World, Welcome to MVC programming...";  
  15.         }  
  16.     }  
  17. }  
So, the next obvious question that comes to your mind is where is this mapping defined?
 
Well, the mapping is defined within the RegisterRoutes() of the RouteConfig class. So, you can find this RouteConfig class within the App_Start Folder.
 
Now, open the RouteConfig.cs class file and you will see that the RegisterRoutes() method of the RouteConfig class has got a default route as shown in the below image.
 
Controllers In ASP.NET MVC Application
  1. A controller in MVC is a class which is inherited from System.Web.Mvc.Controller.
  2. The MVC controller is the one which is going to interact with both Models and views.
  3. The controller class contains a set of public methods which are also called as the action methods. The action methods are going to handle the incoming URL.
  4. In ASP.NET MVC, every controller class name must end with the word “Controller”. For example, the controller for the home page must be HomeController and the controller for an Employee must be EmployeeController.
  5. Every controller class must be located in the Controllers folder.


Similar Articles