This
Controller Layer is the responsible for controlling the application flow and
interaction between the Model and the View. Controllers are the brains of an
ASP.NET MVC application, requesting data from the Model layer and choosing the
correct method of getting that data to the user. Note that the Controller does
not perform any of this work directly, its real job is to delegate as much as
possible to the model and view layers so that they can do the real work. This
article will show you how to create a Controller in ASP.NET MVC 4 application.
Create New Controller
To create a Controller
class, In Solution Explorer right click the Controller folder then select Add
> Controller.
Name your New Controller
Here one thing you should remember that all Controllers must be
named by using “Controller” suffix. In this example I’m going name our
new Controller as “HomeController”. Leave the default template as Empty
MVC Controller.

Notice the Controller
folder in Solution Explorer, A new HomeController.cs class has
been created.

This Controller class
contains action methods that will render View pages. When we have created new
Controller class, it has default action method and it returns View. In most
cases a Controller action method returns a ViewResult.
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace
MVC4_DEMO.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
return View();
}
}
}
ASP.NET MVC invokes
different Controller classes and its action methods depend on the incoming URL.
Pattern: [Controller]/[ActionName]/[Parameters]
Example: http://localhost/Home/Welcome/
The first part of the URl
invokes a Controller class, The second part of the URl invokes the action
method in the Controller class.
Here I’m going to create
some action method and I’ll invoke these methods from browser. First I’ll
replace default Index() action method with return type as string. So it’ll
return a string of HTML. Following example shows that we have created three
action methods with different return values.
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace
MVC_DEMO.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
public string Index()
{
return "This is the default action";
}
//
// GET: /Home/Welcome/
public string Welcome()
{
return "This is the Welcome action
method";
}
//
// GET: /Home/Welcome/parameter
public string User(string id)
{
return "Id=" + id;
}
}
}
First let’s run the application on browser. By default it
invokes index() function on HomeController. So, the question is, where is this
mapping defined. The mapping is defined in Global.asax. Notice that in
Global.asax we have RegisterRoutes() method.
RouteConfig.RegisterRoutes(RouteTable.Routes);
Right click on this method, and select "Go to Definition". Notice the
implementation of RegisterRoutes() method in RouteConfig class.

Let’s invoke Welcome() action method that we have defined in
HomeController class. Now it return value which we have assigned in that action
method.
Then let’s invoke third action method User() with parameter.
Now pass id in the URL as shown below and press enter.

I hope you understand the functionality of the Controller.
Please comment below if is there any confusion or anything I missed to explain
in this article.
Thanks for reading..!! :)