Table of contents
1.
Introduction
2.
MVC Controller
2.1.
Functions of a Controller
2.2.
Working of a Controller
3.
Creating a Controller
4.
Adding a View Page
5.
Frequently Asked Questions
6.
Key Takeaways
Last Updated: Mar 27, 2024

ASP.NET MVC Validations

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

MVC in ASP.NET stands for Model, View, and Controller. MVC separates the user application into three components, i.e., Model, View, and Controller. The controller acts as a mediator between the client-side and the server-side response. It acts as a bridge between the view page and models. The controller deals with the HTTP requests that come from the browser. It retrieves data from the model and renders view as a response. 

In this blog, we will understand ASP.Net MVC Controller, which is one of the most important components of MVC.

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. A controller is a class derived from the base class System.Web.MVC.Controller. The controller class contains public methods known as Action Methods. Each request made by the user is mapped with a particular controller, and each controller has multiple action methods to handle incoming browser requests, retrieve necessary model data and return appropriate responses. 

For example, if a user requests a static page, the controller comes into action and returns the requested page to the user. In ASP.Net MVC, URL doesn’t map with the direct static page. It is mapped with the controller. Every controller class name must have "Controller" as the suffix. E.g., The Homepage controller name must be HomepageController. Every controller class must be located inside the controller folder of the MVC folder structure.

Functions of a Controller

A controller is mainly responsible for performing the following tasks:

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

 

Working of a Controller

 

Source: Link

  1. The user requests through the ASP.Net website.
  2. All these requests then reach the controllers. 
  3. The controller asks the models for data. 
  4. These models further perform database operations and respond to the controller with the required data.
  5. The controller retrieves the data from the model.
  6. Then, the controller renders an HTML page with model data and creates a view page.
  7. Further, the controller responds to the user with the View Page. 

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:

  1. .aspx
  2. .asp
  3. .html
  4. .cshtml
  5. .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

  1. 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.
     
  2. 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.
     
  3. 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.

Live masterclass