MVC Controllers

In the last section, you have learned about the Asp.net MVC application folder structure. In this section, you will learn about the controller part of the Asp.net MVC application.

Controllers are basically the classes which are responsible to handle the user's request. Controllers deal with the flow of an MVC application, they handle the data coming from the user and responds with a relevant View with a specific Model if required. The controllers can validate the user input they can also make changes to the model as per the user requirements.

The User-defined Controller classes inherit the Controller as a base class and mainly they contain the Action methods.

In Asp.Net MVC all the controller classes name must end with a Controller word, as this is a naming convention by Microsoft to create a controller class.

With that, it is also required to put all the controller classes in the Controller folder.


New Controller

To add a new controller to your application, go to Solution Explorer, then inside your project right click on Controller folder, when the menu opens up click on add -> controller in the menu.

Once you will click on Controller option, visual studio will present you dialog called Scaffold as shown below.

Add Scaffold dialog box comes up with multiple templates, from these templates you can choose the required template to create a controller.

In this section, we will keep the things simple and choose the MVC 5 Controller - Empty template. We will discuss other templates in later sections. After selecting the template click on Add button, Visual Studio opens a new dialog box named "Add Controller" as shown below.

In the "Add Controller" dialog box enter the name of controller followed by the Controller word. A controller class must end with a Controller word. Let's give a name to the controller called HomeController and click on Add button.

After Add button pressed visual creates a controller class with a predefined action method named Index and controller class resides in the Controllers folder under the root directory.

HomeController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
 
namespace AspNet_MVC_Tutorials.Controllers
{
    public class HomeController : Controller
    {
        // GET: /Home/
        public ActionResult Index()
        {
            return View();
        }
    }
}

If you will see in the above controller class there is an action method called Index which return type is ActionResult. We will talk about the result types in the next section.

Just to get an idea that how controller and action methods work let's do some changes in the Home controller.

Example

Try It
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
 
namespace MvcApplication1.Controllers
{
    public class HomeController : Controller
    {
        // GET: /Home/
        public string Index()
        {
            return "Hello from Index method of HomeController";
        }
    }
}