Introduction to ASP.NET Core MVC Framework
Introduction to ASP.NET Core MVC Framework
In this article, I am going to give you a brief introduction to ASP.NET Core MVC Framework. Please read our previous article where we discussed Developer Exception Page Middleware Components in ASP.NET Core Application. As part of this article, we are going to discuss the following pointers.
- What is MVC?
- How MVC Design Pattern Works?
- Understanding Model, View, and Controller.
- Where the MVC Design Pattern is used in the real-time three-layer application?
- What is ASP.NET Core MVC?
What is MVC?
MVC stands for Model View and Controller. It is an architectural design pattern that means this design pattern is used at the architecture level of an application.
So, the point that you need to remember is MVC is not a programming language, MVC is not a Framework, it is a design pattern. When we design an application, first we create the architecture of that application, and MVC plays an important role in the architecture of that particular application.
MVC Design Pattern is basically used to develop interactive applications. An interactive application is an application where there is user interaction involved and based on the user interaction some event handling occurred.
The most important point that you need to remember is, it is not only used for developing web-based applications but also, we can use this MVC design pattern to develop the Desktop or mobile-based application.
The MVC (Model-View-Controller) design pattern was introduced in the 1970s which divides an application into 3 major components.
They are Model, View, and Controller. The main objective of the MVC design pattern is the separation of concerns. It means the domain model and business logic are separated from the user interface (i.e., view). As a result, maintaining and testing the application becomes simpler and easier.
How does MVC Design Pattern work in ASP.NET Core?
Let us see an example to understand how the MVC pattern works in the ASP.NET Core MVC application. For example, we want to design an application, where we need to display the student details on a web page as shown below.
The controller is the component in the MVC design pattern, who actually handles the incoming request. In order to handle the request, the controller components do several things are as follows.
The controller component creates the model that is required by a view. The model is the component in the MVC design pattern which basically contains classes that are used to store the domain data, or you can say business data.
In the MVC design pattern, the Model component also contains the required logic in order to retrieve the data from a database.
Once the model created by the controller, then the controller selects a view to render the domain data or model data. While selecting a view, it is also the responsibility of the controller to pass the model data.
In the MVC design pattern, the only responsibility of view is to render the model data. So, in MVC, the view is the component whose responsibility is to generate the necessary HTML in order to render the model data.
Once the HTML is generated by the view, then that HTML is then sent to the client over the network, who initially made the request.
So, the three major components of an ASP.NET Core MVC Application are Model, View, and Controller. Let us discuss each of these components of the MVC design pattern in detail.
Model:
The Model is the component in the MVC Design pattern which is used to manage that data i.e., state of the application in memory.
The Model represents a set of classes that are used to describe the application’s validation logic, business logic, and data access logic. So, in our example, the model consists of Student class and the Student Business Layer class.
public class Student
{
public int StudentID { get; set; }
public string Name { get; set; }
public string Gender { get; set; }
public string Branch { get; set; }
public string Section { get; set; }
}
public class StudentBusinessLayer
{
public IEnumerable<Student> GetAll()
{
//logic to return all employees
}
public Student GetById(int StudentID)
{
//logic to return an employee by employeeId
Student student = new Student()
{
StudentID = StudentID,
Name = "James",
Gender = "Male",
Branch = "CSE",
Section = "A2",
};
return student;
}
public void Insert(Student student)
{
//logic to insert an student
}
public void Update(Student student)
{
//logic to Update an student
}
public void Delete(int StudentID)
{
//logic to Delete an student
}
}
Here, in our example, we use the student class to hold the student data in memory. The Student Business Layer class is used to manage the student data i.e., going to perform the CRUD operation.
So, in short, we can say that a Model in MVC design pattern contains a set of classes that is used to represent the data and also contains the logic to manage those data.
In our example, the student class is the class that is used to represent the data. The Student Business Layer class is the class that is used to manage the student data.
View:
The view component in the MVC Design pattern is used to contain the logic to represent the model data as a user interface with which the end-user can interact.
Basically, the view is used to render the domain data (i.e., business data) which is provided to it by the controller.
For example, we want to display Student data in a web page. In the following example, the student model carried the student data to the view. As already discussed, the one and only responsibility of the view is to render that student data. The following code does the same thing.
@Model ASP Core Application. Models. Student
<html>
<head>
<title>Student Details</title>
</head>
<body>
<br/>
<br/>
<table>
<tr>
<td>Student ID: </td>
<td>@Model.StudentID</td>
</tr>
<tr>
<td>Name: </td>
<td>@Model.Name</td>
</tr>
<tr>
<td>Gender: </td>
<td>@Model.Gender </td>
</tr>
<tr>
<td>Branch: </td>
<td>@Model.Branch</td>
</tr>
<tr>
<td>Section: </td>
<td>@Model.Section </td>
</tr>
</table>
</body>
</html>
Controller:
A Controller is a .cs (for C# language) file which has some methods called Action Methods. When a request comes on the controller, it is the action method of the controller which will handle those requests.
The Controller is the component in an MVC application that is used to handle the incoming HTTP Request and based on the user action, the respective controller will work with the model and view and then sends the response back to the user who initially made the request.
So, it is the one that will interact with both the models and views to control the flow of application execution.
Then that request is mapped to the Details action method of the Student Controller. How it will map to the Details action method of the Student Controller that will discuss in our upcoming articles.
public class Student Controller: Controller
{
public Action Result Details (int student)
{
Student Business Layer student BL = new Student Business ).
Student studentDetail = studentBL.GetById(studentId);
return View(studentDetail);
}
}
As you can see in the example, the Student Controller creates the student object within the Details action method. So, here the student is the Model. To fetch the student data from the database, the controller uses the Student Business Layer class.
Once the controller creates the student model with the necessary student data, then it passes that Student model to the Details view. The Details view then generates the necessary HTML in order to present the student data. Once the HTML is generated, then this HTML is sent to the client over the network who initially made the request.
Note: In the MVC design pattern both the Controller and View depend on the Model. But the Model never depends on either view or controller. This is one of the main reasons for the separation of concerns. This separation of concerns allows us to build the model and test independently of the visual presentation.
Where MVC is used in the real-time three-layer application?
In general, a real-time application may consist of the following layers
- Presentation Layer: This layer is responsible for interacting with the user.
- Business Layer: This layer is responsible for implementing the core business logic of the application.
- Data Access Layer: This layer is responsible for interacting with the database to perform the CRUD operations.
The MVC design pattern is basically used to implement the Presentation Layer of the application. Please have a look at the following diagram.
What is ASP.NET Core MVC?
The ASP.NET Core MVC is a lightweight, open-source, highly testable presentation framework that is used for building web apps and Web APIs using the Model-View-Controller (MVC) design pattern. So, the point that you need to remember is, MVC is a design pattern and ASP.NET Core MVC is the framework that is based on MVC Design Pattern.
The ASP.NET Core MVC Framework provides us with a patterns-based way to develop dynamic websites and web apps with a clean separation of concerns. This ASP.NET Core MVC framework provides us the full control over the mark-up. It also supports for Test-Driven Development and also uses the latest web standards.
In the next article, we are going to discuss how to set up the MVC middleware in asp.net core application. In this article, I try to give a brief introduction to ASP.NET Core MVC Framework. I would like to have your feedback. Please post your feedback, question, or comments about this ASP.NET Core MVC framework article.
Leave a Comment