Controller in ASP.NET MVC

The controller is like the heart of MVC which takes care of communication between model and view.It respond to user actions.Based on user action it communicates between model and view.Finally it chooses view to display the user interface to Viewers.
It is the hero of MVC. The controller communicates with model layer and pass data to View. So that final output is rendered in HTML Page.
The controller data can be accessed in VIEW using VIEWDATA,VIEWBAG,TEMPDATA.;


How to add controller in ASP.NET MVC Project?


Goto Solution Explorer and right click Controllers folder --> add --> controller


Now it displays scaffold dialog, It has lot of templates,you can select your own template depending upon your choice,So i am going for basic mvc controller template
Scaffolding: Scaffolding is an automatic code generation framework for ASP.NET web applications. Scaffolding reduces the time taken to develop a controller, view etc.


Now give your page name, for example i am going to create employee page.so i have give the name as "Employeecontroller".So its your choice you can give your own name.


This will create Employeecontroller class in codebehind like the below code snippet:-

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

I am going to display "This is an Employee page" when i run the url in the browser,How to do it?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace LearnMVCEasy.Controllers
{
public class EmployeeController : Controller
{
// GET: Employee
public String Index()
{
    return "THIS IS AN EMPLOYEE PAGE";
}
}
}

You can call like this http://localhost/Employee and see the output like the below screenshot