Creating a Controller
This section will understand how the controller and view page work together.
- Go to the menubar, click on the file and create a new project.

-
Choose ASP.NET Web Application (.NET Framework) as your project template.

-
Configure your project, assign it with a name and create the project.

-
Make sure that you press the right choices, i.e., An empty template, no authentication, and checkmark the MVC under the add folders & core references heading. Refer to the image given below.

-
Go to visual studio, right-click on the controller folder -> Add -> controller.

This will open an “Add scaffold” dialog box.
(Scaffolding is an automatic code generation framework for ASP.Net web applications. Scaffolding reduces the time to develop a controller, view, etc., in the MVC framework.)
- Select MVC 5 Controller- empty and click on the add button.

-
Then, give the controller name to the ItemController. “Controller” suffix must be added in the controller name. Every controller must be located in the controller folder. Here, we are assigning the name Ninja to our controller. Further, click on the add button to add the controller.

-
This will create the NinjaController class with the Index() method in NinjaController.cs file under the controller folder.

-
The NinjaController class is derived from the Controller class. A controller in MVC must be derived from the abstract controller class. This base controller class also contains helper methods that can be used for multiple purposes.
- Now, we can return a dummy string from the index action method of the controller that we have defined. Change the return type of the index method from ActionResult to string and return any dummy string of your choice. Refer to the image given below.

(If you face any issues while debugging, try changing the default controller name from “Home” to “your file name” in the RouteConfig.cs file under App_Start folder.)
Output:

Adding a View Page
A view in the ASP.Net MVC is responsible for the user interface. The view displays the data coming from the models. It is an HTML template that binds and displays HTML controls with data. The “.cshtml” file uses the Razor view engine, and .cshtml views are used in C# programming. It is a combination of C# and HTML (.cshtml). The return type can either be “ViewResult” or “ActionResult.”
A view can contain the following extension depending on the language:
- .aspx
- .asp
- .html
- .cshtml
- .vbhtml
Let’s create a view page to understand the concept better. Refer to the following steps:
-
Right-click on the index action method and select add view option from the dialog box.

-
A dialog box named add view will appear on the screen, as shown below. Click on the MVC5 view and press the add button to add the view page.


-
An index view page will be added named “filename.cshtml”. In this case, it is Index.cshtml.

The view file initially will look something like this:

-
Add the following code to the NinjaController.cs file.

- Add the following code in Index.cshtml file.
<h1>@ViewBag.NinjaList</h1>
-
Now, run the project (or press Ctrl + F5). You will see a window popping up which will contain your desired output.
Output:

If you pay attention to the URL given below:

Note that the controller will execute on the above link. Here, Ninja represents the controller, and Index represents the action method. The incoming URL in an ASP.Net MVC application is mapped to the controller action method.
Frequently Asked Questions
-
What do “beforeFilter()”,”beforeRender”, and “afterFilter” functions do in a controller?
beforeFilter(): It runs before every action in the controller. It is the right place to inspect user permissions or check an active session.
beforeRender(): It is called after the controller action logic before the view is rendered. This function is not used frequently but may be required if a user calls render() manually before the end of a given action.
afterFilter(): It is called after every controller action and after rendering is done. It is the last controller method to run.
-
What is the role of Presentation, Abstraction, and Control components in MVC?
Following are the roles of components Presentation, Abstraction, and Control in MVC:
Presentation: The visual representation of a particular abstraction within the application.
Abstraction: It is the business domain functionality within the application.
Control: It is a component that keeps a consistency between the abstraction within the system and its presentation to the user. In addition to communicating with other controls within the system.
-
Explain routing and its three segments?
Routing helps decide a URL structure and map the URL with the controller. The three segments that are important for routing are the following:
ControllerName, ActionMethodName and Parameter
Key Takeaways
This blog taught us about ASP.Net MVC Controller. The Controller in MVC handles any incoming URL request. It is responsible for handling user requests that come from the ASP.Net website. The Controller class name must have "Controller" as its suffix.
If you are a beginner and interested in learning DSA, web development, or competitive programming, you can follow our guided path to get a good grip on such concepts.