Please tell me about asp.net MVC controller and how we create it with an example and an image.
What is Asp.Net MVC Controller?
43030-Jan-2020
Updated on 30-Jan-2020
Home / DeveloperSection / Forums / What is Asp.Net MVC Controller?
Please tell me about asp.net MVC controller and how we create it with an example and an image.
Nishi Tiwari
30-Jan-2020ASP.NET MVC Controller
The controller is defined as a class that handles user requests. It retrieves data from the Model and renders view as a response.
In ASP.NET MVC framework the requested URLs are mapped to the classes which are referred to as controllers. The controller processes the incoming requests handles user inputs and interactions and executes appropriate business logic.
The ControllerBase class is a base class for all the controller classes. It provides general MVC handling. A controller usually performs the following tasks.
o It locates for the appropriate action method to call and validate it.
o It gets the values so as to use the action method's arguments.
o It handles all errors which might occur during the execution of the action.
o It uses the WebFormViewEngine class for rendering the ASP.NET page.
All controller classes must be named with the "Controller" suffix.
Create a Controller
You can create a controller for an application by adding a new item to the controller folder. First right click on the controller folder and click add -> controller as shown below.
Provide controller name and then click Add.
After adding the controller project will create a folder with the same name as the controller name in the view folder so as to store the view files belongs to the controller.
The controller will contain the default code like below.
// MusicStoreController.cs
To access the controller we add an index file to the MusicStore folder inside the view folder. The index file will contain the following code as given below.
// index.cshtml
Output:-