Controllers in MVC : Overview of Controller

Introduction

This article provides an overview of the Controller in MVC. As you know, MVC stands for Model , View and Controller; so, Controller plays a very important role in MVC. We'll learn the controller actions and action results in here and how the controllers are used to interact with the MVC applications.

Overview

The controller in MVC is responsible for responding the request made against the ASP.NET MVC Web Application. Each browser request is mapped to a specific controller that is defined in the MVC application.

Suppose you are entering the following URL in the browser:

http://localhost:1234/Sample

It means that the Controller is called SampleController. The browser is making a request to the controller so now the SampleController is responsible for providing the response to the browser. It might be possible that the controller returns a specific view back to the browser or the controller might redirect the user to another controller.

Getting Started

Let us create a Controller in a MVC application. To create a controller you must have the MVC application so at first we'll create the MVC application and then controller. Just follow the procedure below.

Step 1: Create an ASP.NET Web Application and enter the name as in the following:

Creating Web Application in VS 2013

Step 2: Select MVC Project Template in the One ASP.NET Wizard.

MVC Project Template in VS 2013

Step 3: In the Solution Explorer, right-click on the Controllers folder then click on "Add" -> "Controller...".

Adding New Controleer in MVC

Step 4: Now select the MVC Empty Controller.

Adding MVC Empty Controller

And enter the name as Sample as in the following:

Add Controller in MVC

After entering the controller name, using scaffolding there is a folder named Sample (the same name as the newly added controller) will be added automatically in the Views folder. Have a look:

Solution Explorer in MVC

Step 5: You can see the newly added controller has code as in the following:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

 

namespace DemoMvc.Controllers

{

    public class SampleController : Controller

    {

        // GET: Sample

        public ActionResult Index()

        {

            return View();

        }

    }

}

In the code above, you can see that the controller is created in the form of a class so you can say that it is just a class that is derived from the Controller class. This class inherits the Controller class (base class). In the next section, we'll understand the actions.

Overview of Actions in Controller

The actions are disclosed by the controller. An action is a method on a controller that is called when you enter a specific URL in the browser. Suppose you enter the following URL in the browser:

http://localhost:1234/Sample/Index

The Index() method is called through the preceding URL. This Index() method is stored in the SampleController. It is necessary that the access modifier of the action method must be public. This action method of any controller cannot be overloaded further and also cannot be a static method.

Overview of Action Results

As you can see in the preceding action method that the return type of that Index() action method is ActionResult, so an action method returns an action result. An action result is what a controller action returns in response to a browser request.

There are various types of action results supported in the MVC, as given below:

  • ViewResult: Represents HTML and markup.
  • EmptyResult: No Result
  • RedirectResult: a redirection to a new URL
  • JsonResult: a JSON result that is used in ajax app
  • JavaScriptResult: a JavaScript result
  • ContentResult: a text result

In most cases the controller action returns a ViewResult as in the following:

public class SampleController : Controller

{

    // GET: Sample

    public ActionResult Index()

    {

        return View();

    }

}

You can see in the code above that the View is used to return a ViewResult. When this is returned, HTML is returned to the browser. You can see the methods of the controller base class:

  • View: Returns a ViewResult action result.
  • Redirect: Returns a RedirectResult
  • JSON: Returns a JsonResult
  • JavaScript: Returns a JavaScriptResult
  • RedirectToAction: Returns a RedirectToActionResult

So, suppose you want to redirect from one controller to another controller, you need to use the RedirectToAction() method. Have a look:

using System.Web.Mvc;

 

namespace DemoMvc.Controllers

{

    public class SampleController : Controller

    {

        // GET: Sample

        public ActionResult Index()

        {

            return View();

        }

 

        public ActionResult SampleWorld()

        {

            return RedirectToAction("Welcome");

        }

        public ActionResult Welcome()

        {

            return View();

        }

    }

}

Suppose we want to use the ContentResult action result because it is special. It returns an action result as simple plain text. Have a look:

using System.Web.Mvc;

 

namespace DemoMvc.Controllers

{

    public class SampleController : Controller

    {

        // GET: Sample

        public ActionResult Index()

        {

            return View();

        }

 

        public ActionResult Welcome()

        {

            return Content("Hello and Welcome to MVC");

        }

    }

}

Adding View

You can add a view for every controller in a very easy way. Have a look at the following procedure.

Step 1: Just right-click on the controller and select Add View

Adding View of Controller in MVC

Step 2: Just click on Add in the next wizard

Add View in MVC

Step 3: When you run the Welcome View, you can see the following screenshot

Displaying View on Browser

You can check out in the browser URL that the Welcome controller action method is called of the SampleController. That's it.

Summary

This article provided an overview of the controller, controller actions and action results of ASP.NET MVC. In here you learned the overview of controllers, next we'll learn the custom routes in controllers. Thanks for reading.

Next Recommended Readings