Controllers

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?

Now, let's add a new empty controller in asp .net mvc project in visual studio 2019.
In the Visual Studio, right click on Controller folder -> Select Add -> click on controller

adding_controller

This opens Add Scaffold dialog

scaffold_dialoge


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

default_controller


In the Add Controller dialog, enter the name of the controller. Remember, the controller name must end with Controller. Write StudentController and then click on Add.

student_controller

This will create the StudentController class with the Index() method in the StudentController.cs under the Controllers folder.

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();
        }
    }
}

The StudentController class is derived from the Controller class, as you can see above. Every controller in MVC must be derived from this abstract Controller class. This base Controller class has a number of helper methods that can be used for a various purposes.

We'll now return a dummy string from the StudentController's Index action method. The following example shows changing the return form of the Index method from ActionResult to string and returning a dummy string. In the next part, you'll hear about the ActionResult.

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"; } } }
Now run the application.
output

You can see the output in above image.

Summary

  1. 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.
  2. All the public methods in Controller class are called Action methods.
  3. The Controller class must be derived from System.Web.Mvc.Controller class.
  4. The name of the Controller class must end in "Controller."
  5. Various scaffolding templates may be used to build a new controller. You can also make your own scaffolding template.
It was the summary of Controllers of ASP .Net MVC I hope you enjoyed and learned it, in case of any question, confusion or suggestion do comment below I would be happy to help you.

Happy Coding!✌




Post a Comment

0 Comments