ASP.NET MVC Controller

Controller is a class that handles user requests. It retrieves data from the Model and renders view as response.

The ASP.NET MVC framework maps requested URLs to the classes that are referred to as controllers. Controller processes incoming requests, handle user input and interactions and executes appropriate business logic.

The ControllerBase class is a base class for all controller classes. It provides general MVC handling. A controller mainly performs the following tasks.

  • It locates for the appropriate action method to call and validate.
  • It gets the values to use as the action method's arguments.
  • It handles all errors that might occur during execution of the action.
  • It uses the WebFormViewEngine class for rendering ASP.NET page.

Note: All controller classes must be named by using the "Controller" suffix.


Create a Controller

We can create controller for the application by adding a new item into the controller folder. Just right click on the controller folder and click add -> controller as given below.

ASP Mvc controller 1

Providing controller name, then click Add.

ASP Mvc controller 2

After adding this controller, as per the conventions project will create a folder with the same name as the controller name in the view folder to store the view files belongs to the controller.

This controller contains the default code like below.

// MusicStoreController.cs

To access this controller to the browser, we are adding an index file to the MusicStore folder inside the view folder. This index file contains the following code.

// index.cshtml

Run this file in non-debug mode by pressing Ctrl+F5. This will produce the following output.

ASP Mvc controller 3




Contact US

Email:[email protected]

MVC Controller
10/30