Any incoming URL request is handled by the Controller in MVC architecture. The Controller is a class that is derived from the System.Web.Mvc.Controller base class. Action methods are public methods in the Controller class. Incoming browser requests are handled by the controller and its action method, which retrieves required model data and returns appropriate responses.
Every controller class name must end with the word "Controller." The name of the home page controller must be HomeController, while the name of the student page controller must be StudentController. Every controller class must also be located in the Controller folder of MVC folder structure.
How to add a new controller?
Add Scaffold dialog consist of different templates to create a new
controller.select "MVC 5 Controller - Empty" and then click on Add. It will open the Add Controller dialog, as shown below
Controller. Write StudentController and then click on Add.using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace TestProject.Controllers
{
public class StudentController : Controller
{
// GET: Student
public ActionResult Index()
{
return View();
}
}
}using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace TestProject.Controllers
{
public class StudentController : Controller
{
// GET: Student
public string Index()
{
return "Index action of Student controller is called";
}
}
}Summary
- Incoming URL requests are handled by the Controller. Based on the URL and configured Routes, MVC routing sends requests to the required controller and action method.
- All the public methods in Controller class are called Action methods.
- The Controller class must be derived from System.Web.Mvc.Controller class.
- The name of the Controller class must end in "Controller."
- Various scaffolding templates may be used to build a new controller. You can also make your own scaffolding template.




0 Comments