3sep2024 CoreMVC2
3sep2024 CoreMVC2
A Controller is a special class in the ASP.NET Core Application with .cs (for C#
language) extension. In the ASP.NET Core MVC Application, the controller class
must be inherited from the Controller base class. In ASP.NET Core MVC, the
Controller base class provides many properties, methods, and features that
handle HTTP requests and produce responses in our application. It provides Action
Result Methods, Model State Management, Validation, TempData, ViewBag,
ViewData, etc. A controller is a class that:
Controllers in the MVC Design Pattern handle the incoming HTTP Request, work
with the model, and select a view to render. When the client (browser) sends a
request to the server, that request first goes through the request processing
pipeline. Once the request passes the request processing pipeline (i.e.,
Middleware Component Registered into the Pipeline), it will hit the controller.
Inside the controller, there are lots of methods (called action methods) that
handle the incoming HTTP Requests. The action method inside executes the
business logic and prepares the response, which is sent back to the client who
initially requested it. For a better understanding, please have a look at the
following diagram.
The Controllers in the ASP.NET Core MVC Application logically group similar types
of actions together. This grouping of similar types of action together allows us to
define sets of rules, such as caching, routing, and authorization, which will be
applied collectively.
1
How do you add Controllers to the ASP.NET Core Application?
If you create the ASP.NET Core Application using the MVC Project Template, it will
create HomeController within the Controllers folder by default. But if you create
the ASP.NET Core Application with the Empty Project template, then by default,
you will not find the Controllers folder in your project. As we discuss everything
from scratch, we will create the ASP.NET Core Application with Empty Template
and manually add the Controllers folder and the Controllers.
To create a new Empty ASP.NET Core Web Application, open Visual Studio 2022
and click the Create a new project tab, as shown in the image below.
Once you click on the Create a new project tab, the Create a new project window
will open. Select the ASP.NET Core Empty project template in this window and
click the Next button, as shown in the image below.
Once you click on the Next button, it will open the Configure Your New Project
window. Here, you must provide the necessary information to create a new
2
project. First, give an appropriate name for your project
(FirstCoreMVCWebApplication), set the location where you want to create this
project, and the solution name for the ASP.NET Core Web application. And finally,
click on the Create button, as shown in the image below.
Once you click on the Next button, it will open the Additional Information window.
Here, you need to select .NET Framework. You also need to check the Configure
for HTTPS and do not use top-level statements check boxes. Finally, click the
Create button, as shown in the image below.
Once you click the Create button, a new ASP.NET Core Web Application will be
created in Visual Studio 2022 using .NET 8. The project will have the following file
and folder structure.
3
Step 2: Adding Controllers Folder
Once you create the ASP.NET Core Empty Project, we need to add the Controllers
folder to create our Controllers. To do so, right-click on the project and then select
the add => new folder option from the context menu, which will add a new
folder to your project. Just rename the folder as Controllers.
As you can see in the above image, we have three templates for creating an MVC
controller. So you can use any of the following three templates:
4
Once you click the Add button, StudentController will be Added to the Controllers
folder, as shown in the image below.
Understanding StudentController:
Now, let us understand the StudentController class and its different components.
Open the StudnetController.cs class, and you should get the following default
code.
5
As you can see in the above image, the StudentController class is inherited from
the Controller base class. This controller base class is present in
Microsoft.AspNetCore.Mvc namespace, which is why it imports
that Microsoft.AspNetCore.Mvc namespace. Now right-click on the Controller
base class and select Go to the definition, and you will see the following definition
of the Controller class.
As you can see in the above image, the Controller is an abstract class having
many methods (Json, View, PartialView, OnActionExecuting, ViewComponent, etc.)
and properties (TempData, ViewBag, ViewData, etc.). The point that you need to
remember is that these methods and properties will be used when we are working
with ASP.NET Core MVC Application. Again, if you look, this Controller class is
inherited from the ControllerBase class.
6
Routing, Model Binder, HttpContext, and many more properties and methods,
which we will use as part of our ASP.NET Core MVC Application.
All the public methods of a controller class are known as Action Methods. They are
created for a specific action or operation in the application. The Controller class
can have many related action methods. For example, adding a Student is an
action. Modifying the student data is another action. Deleting a student is another
action. So, all the related actions should be created inside a particular controller.
An action method can return several types. Let us modify the HomeController as
shown below, where one method returns all the student details. Intentionally, we
returned a string from this method, we return a string just for learning purposes.
using Microsoft.AspNetCore.Mvc;
namespace FirstCoreMVCWebApplication.Controllers
{
public class StudentController : Controller
{
public string GetAllStudents()
{
return "Return All Students";
}
7
}
}
How Do We Call an Action Method of a Controller?
When our application receives an HTTP Request, the controller action method
handles it. So, when we say we are hitting a controller, we are hitting its action
method. The default structure
is http:domain.com/ControllerName/ActionMethodName.
As we are working with the development environment using Visual Studio, the
domain name will be our local host with some available port numbers. So, if we
want to access the GetAllStudents action method of the HomeController, then the
URL: http://localhost:<portnumber>/student/GetAllStudents
Let us prove this. You will not get the output if you run the application and
navigate to the above URL. This is because we created this project using the
ASP.NET Core Empty Project template. By default, the Empty Project template will
not add the required MVC Service and Middleware Components to the application
processing pipeline.
Open the Program.cs class file and then copy and paste the code below into it,
adding the required MVC service to the dependency injection and the MVC
middleware to the request processing pipeline.
namespace FirstCoreMVCWebApplication
{
public class Program
{
public static void Main(string[] args)
{
// Create a WebApplication builder, which provides various configuration settings
// and services for the web application
var builder = WebApplication.CreateBuilder(args);
// Add MVC services to the service container.
// This includes support for controllers and views.
builder.Services.AddMvc();
// Build the application using the configured builder
var app = builder.Build();
// Enable routing middleware, which matches incoming HTTP requests to
endpoints defined in the application
app.UseRouting();
// Map the default controller route (convention:
{controller=Home}/{action=Index}/{id?})
8
// This means if no specific route is provided, it will default to HomeController and
Index action
app.MapControllerRoute(
name: "default", // Name of the route
pattern: "{controller=Home}/{action=Index}/{id?}" // URL pattern for the route
);
// Run the application, which blocks the calling thread and starts listening for
incoming HTTP requests
app.Run();
}
}
}
With the above change in place, now run the application and navigate to the
URL http://localhost:<portnumber>/student/GetAllStudents, and you
should get the output as expected, as shown in the image below.
Please look at the following image to better understand how the above URL
mapped to the Student Controller’s GetAllStudents action method.
Let us understand this with an example. Now, we want to search for students
based on their names. To do so, add the following action method inside the
Student Controller.
9
public string GetStudentsByName(string name)
{
return $"Return All Students with Name : {name}";
}
Now run the application, navigate to the
URL http://localhost:<portnumber>/student/GetStudentsByName?
name=james, and see the output as shown in the below image.
In this case, the query string parameter name is mapped with the
GetStudentsByName action method name parameter.
To create an ASP.NET Core MVC Application, we need to add the required MVC
Services and Middleware Components into the Request Processing Pipeline. For
example, you can add the MVC services using the following statement within your
Main method of the Program.cs class file.
10
AD
Then, we need to configure the MVC Middleware into the Request Processing
Pipeline. For example, the following code will add the MVC Middleware Component
to the Application Processing Pipeline.
So, in the ASP.NET Core MVC Web Application, the MVC Middleware Component
receives an HTTP Request when the client sends an HTTP Request. Once the MVC
Middleware Component receives the request, based on routing, it selects the
controller and action method to execute.
However, in order to execute the action method, the MVC Middleware must create
an instance of the selected controller. This makes sense, as we know that if we
want to invoke a non-static method, we need an instance of the class. This is not
different from executing a controller action method.
The MVC Middleware uses Reflection to create an instance of the Controller class.
This will use the following IControllerActivator class. As you can see, this
interface provides three methods: Create, Release, and ReleaseAsync.
11
object Create(ControllerContext context):
12
As you can see, this class provides implementations for the IControllerActivator
interface methods. This is how the controller instance is created in the ASP.NET
Core MVC Application.
13