Introduction to ASP.NET Core MVC Framework

Introduction to ASP.NET Core MVC Framework

In this article, I will briefly introduce the Model View Controller Pattern and ASP.NET Core MVC Framework. MVC is a Design Pattern, and ASP.NET Core MVC is a framework based on the MVC Design Pattern. As part of this article, we will discuss the following pointers.

  1. What is MVC?
  2. How Does MVC Design Pattern Work in ASP.NET Core?
  3. What is ASP.NET Core MVC?
  4. Features of ASP.NET Core MVC
  5. When to Choose ASP.NET MVC and When to Choose ASP.NET Core MVC?
What is MVC?

MVC stands for Model View and Controller. It is an Architectural Design Pattern, which means it is used at the application’s architecture level. So, MVC is not a programming language, not a Framework. It is a Design Pattern. When we design an application, we first create its architecture, and MVC plays an important role in designing that architecture.

The MVC Design Pattern is used to develop interactive applications. An interactive application involves user interaction, and based on the user interaction, some event handling occurs. The most important point you need to remember is that it is not only used for developing Web-Based Applications; we can also use this MVC Design Pattern to develop Desktop or Mobile-Based applications.

The MVC (Model-View-Controller) Design Pattern was introduced in the 1970s. It divides an application into three major components: Model, View, and Controller. The main objective of the MVC Design Pattern is the separation of concerns. This means the Domain Model and Business Logic are separated from the User Interface (i.e., View). As a result, maintaining and testing the application become simpler and easier.

How Does MVC Design Pattern Work in ASP.NET Core?

Let’s look at an example to understand how the MVC Design Pattern works in an ASP.NET Core MVC application. We want to design an application that displays the student details on a web page, as shown below.

ASP.NET Core MVC

So, when we request something like https://localhost:7132/Student/Details/2 from a web browser, the following things happen to handle the request.

ASP.NET Core MVC application

The Controller is the Component in the MVC design pattern that handles the incoming request. The controller components do several things to handle the request. 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 to retrieve data from a database.

Once the controller creates the model, it selects a view to render the domain or model data. While selecting a view, it is also the controller’s responsibility to pass the model data. In the MVC design pattern, the view’s only responsibility is rendering the model data. So, in MVC, the view is the component responsible for generating the necessary HTML to render the model data. Once the view generates the HTML, that HTML is sent to the client over the network who initially made the request.

The three major components of an ASP.NET Core MVC Application are the Model, View, and Controller. Let’s discuss each component of the MVC design pattern in detail.

Role of Model in MVC Design Pattern:

The Model in an MVC application represents the application’s state and business logic. That means the Model is the component in the MVC Design pattern used to manage the data, i.e., the application’s state in memory. The Model represents a set of classes used to describe the application’s validation, business, and data access logic. So, in our example, the model consists of Student and StudentBusinessLayer classes. 

Student.cs
namespace ASPNETCoreMVCApplication.Models
{
    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; }
    }
}
StudentBusinessLayer.cs
namespace ASPNETCoreMVCApplication.Models
{
    public class StudentBusinessLayer
    {
        public IEnumerable<Student> GetAll()
        {
            //logic to return all employees
            return new List<Student>();
        }
        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 a student
        }
        public void Update(Student student)
        {
            //logic to Update a student
        }
        public void Delete(int StudentID)
        {
            //logic to Delete a student
        }
    }
}

In our example, we use the Student class to hold the student data in memory. The StudentBusinessLayer class manages the student data, performs the CRUD operation, Validates the Student data, etc.

So, in short, we can say that a Model in the MVC Design Pattern contains a set of classes used to represent the data and the logic to manage those data. In our example, the Student class represents the data. The StudentBusinessLayer class manages the student data, validates it, and stores it in the database.

Key Responsibilities of Models:
  • Retrieve and store data in the database or other storage mechanisms.
  • Provide data to the controller as needed.
  • Respond to instructions from the controller to update itself (e.g., updating data points).
Role of View in MVC Design Pattern: 

The View is the Component in the MVC Design pattern that contains the logic to represent the model data as a user interface with which the end-user can interact. Basically, the view renders the domain data (i.e., business data) provided by the controller. There should be minimal logic (you should not write any business logic, calculation logic, validation logic, etc.) within views, and any logic in them should only be related to presenting the content. 

For example, we want to display Student data on a web page. The student model carried the student data to the view. As already discussed, the view’s one and only responsibility is to render the model data, in this case, student model data. The following code does the same thing.

@model ASPNETCoreMVCApplication.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>
Key Responsibilities of Views:
  • Display the data provided by the controller in a format suitable for interaction.
  • Send user inputs (like button clicks or data entries) to the controller.
  • Update the visual presentation when the data in the model changes.
Role of Controller in MVC Design Pattern:

The Controller is the component in an MVC application that handles the incoming HTTP Request. Based on the user action, the respective controller might work with the model, select a view to render the information, and then send the response back to the user who initially made the request. So, the Controller is the component that will interact with both the models and views to control the application execution flow. 

In ASP.NET Core MVC Application, a Controller is a .cs (for C# language) file with some methods called Action Methods. When a request comes on the controller, the controller’s action method handles those requests. In our example, when the user issued a request, the following URL

https://localhost:7132/Student/Details/2

Then, that request is mapped to the Details action method of the Student Controller using the Routing defined for our application. We will discuss Routing in detail in our coming articles. For now, look at the following Controller code:

using ASPNETCoreMVCApplication.Models;
using Microsoft.AspNetCore.Mvc;

namespace ASPNETCoreMVCApplication.Controllers
{
    public class StudentController : Controller
    {
        public ActionResult Details(int studentId)
        {
            StudentBusinessLayer studentBL = new StudentBusinessLayer();
            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. The controller uses the StudentBusinessLayer class to fetch the Student data from the database. 

Once the controller creates the Student model with the necessary student data, it passes the Student model to the Details view. The Details view then generates the necessary HTML to present the Student data. Once the HTML is generated, it is sent to the client over the network who initially made the request.

Key Responsibilities of Controllers:
  • Handle user input and convert it to commands for the Model or View.
  • Decide what response to send back to a user when a user makes a browser request.
  • Update the Model when the user manipulates it through the View.
  • Select the appropriate View to present the data from the Model.

Note: The Controller and View depend on the Model in the MVC Design Pattern, but the Model never depends on either View or Controller. This is one of the main reasons for the separation of concerns, which allows us to build and test the model independently of the visual presentation.

What is ASP.NET Core MVC?

ASP.NET Core MVC is a web application development framework developed by Microsoft. It is a modern implementation of the Model-View-Controller (MVC) architectural pattern designed to build dynamic web applications and web APIs. It is part of the ASP.NET Core platform, a cross-platform, high-performance framework for building modern, cloud-based, and internet-connected applications. So, the point that you need to remember is that MVC is a Design Pattern, and ASP.NET Core MVC is a Framework based on the MVC Design Pattern.

Features of ASP.NET Core MVC:

The ASP.NET Core MVC Framework comes with some amazing features. They are as follows:

  • Open Source: The ASP.NET Core MVC Framework is open source, which is the main reason for its popularity. The Entire Source Code of ASP.NET Core MVC Framework is available at https://github.com/aspnet, and you are free to download the source code; even if you want, you can also modify and compile your own version.
  • Built-In Dependency Injection: ASP.NET Core has built-in support for dependency injection, which helps manage dependencies between objects, making the system more modular, scalable, and testable.
  • Rich Routing: ASP.NET Core MVC provides a robust routing mechanism that allows developers to control web application URLs. It supports pattern-based URL mapping, which helps define SEO-friendly URLs.
  • Support for Web APIs: ASP.NET Core MVC can be used to develop RESTful HTTP services. It fully supports formatting response data as JSON or XML and can easily handle different content types with content negotiation.
  • Extensive Built-In Middleware: The framework provides extensive built-in middleware components that can handle requests for authentication, routing, session state, and more. Developers can also create custom middleware.
  • Tag Helpers: Tag Helpers enable server-side code to participate in creating and rendering HTML elements in Razor files. 
When to Choose ASP.NET MVC and When to Choose ASP.NET Core MVC?

Choosing between ASP.NET MVC and ASP.NET Core MVC depends on various factors, including project requirements, existing infrastructure, performance needs, and future maintenance considerations. The following are some of the guidelines to help you decide:

ASP.NET MVC:
  1. If you are currently working on an existing application with ASP.NET MVC and would like to expand the functionalities by adding new features.
  2. If your team is familiar with ASP.NET MVC Framework but has yet to gain experience with ASP.NET Core MVC.
  3. If you want, your application will only be compatible with devices and servers that run on the Windows operating system.
ASP.NET Core MVC:
  1. If you have a preference for utilizing a framework that is completely open source.
  2. If you want your application to be able to be developed and hosted on any operating system.
  3. If your team members have knowledge of ASP.NET Core MVC.
  4. If you are looking for an application development framework with a long development roadmap ahead of it, look at .NET’s roadmap. Microsoft has already provided it for the next five years.

In the next article, I will 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 want your feedback. Please post your feedback, questions, or comments about this ASP.NET Core MVC framework article.

10 thoughts on “Introduction to ASP.NET Core MVC Framework”

  1. blank

    Please correct : Where MVC is used in real-time there layer application?
    to
    Where MVC is used in real-time three layer application?

  2. blank

    This is raviteja from hyderabad.I learned ASP.NET CORE MVC from this website.It is really really great tutorials that are very easy to understand and very informative.Please update the remaining concepts of ASP.NET CORE MVC.I am eagerly waiting for that.I will share this website to all my friends..

  3. blank

    Thanks for the detailed explanation.I have learned all the .NET framework related concepts in your tutorial.
    Now .NET team released .NET5. Can we use all these concepts in .NET 5 also? I am eagerly waiting for your reply.

  4. blank

    Your training has not only expanded my skill set but has also inspired me to set higher goals for myself. I feel fortunate to have had the opportunity to learn under your guidance.

  5. blank

    I want to ask which version of .NET Core these tutorials are for? Because .NET 8 is the latest version and .NET 9 has been released. So, are these tutorials outdated?

Leave a Reply

Your email address will not be published. Required fields are marked *