ASP.NET

Overview of MVC architecture in ASP.NET

This article will briefly provide an Overview of MVC architecture in ASP.NET. Basically, MVC (Model View Controller) is an architectural pattern for creating an application. With this pattern, the application is designed using three entities – the Model, the View, and the Controller. While the Model represents the data maintained by the application, View provides the presentation and implements the UI (User Interface). Further, the Controller part provides the communication between the Model and the View.

This architectural pattern has been widely adopted for implementing web applications.

The model basically represents the data and behavior for the application and is usually independent of the user interface (UI). It contains classes that have properties and methods. These classes are reusable. It means they can be used in entirely different applications such as a mobile application or a desktop application.

Since View represents the user intefcae, for a web application it contains the HTML markup that displays to the user.

Basically, the Controller handles the HTTP Requests. Whenever the user visits a web page, a request has been generated and it is first sent to the Controller object. Methods of a controller are called actions.

The controller, in turn fetches the data from the Model and places that data inside a View.

In fact, the Model View Controller (MVC) pattern better exhibits the separation of concerns. Likewise, applications built using this pattern are more maintainable.

Besides, Model, View, and Controller, there is one more significant element of an MVC application, and it is known as a router. In fact, a Router is an integral part of an MVC application and its role is to correctly select a Controller.

Now that, we have an understanding of MVC Architectural Pattern, so let us create an application for demonstrating this concept.

Creating an MVC Application in ASP.NET

As an illustration of MVC concept, we create a new project in ASP.NET using Visual Studio 2019 as shown below.

Overview of MVC architecture in ASP.NET
Create an MVC Application in ASP.NET

It is important to remember that ASP.NET Core Web App (Model-View-Controller) is selected.

Creating ASP.NET Core Web Application
Creating ASP.NET Core Web Application

After that, we add a controller in the application as shown below. For the purpose of adding a controller, select the Solution Explorer from the View menu and right-click on the controllers. Finally, add the controller and give it a suitable name as shown below.

Adding a Controller
Adding a Controller

The following image shows adding an empty controller.

Add an Empty MVC Controller
Add an Empty MVC Controller

Also, specify the name of the controller.

Create a Controller with name StudentController
Create a Controller with name StudentController

Thereafter, open the StudentController.cs file and define few methods in the StudentController class. Basically, the controller class can have public methods and these public methods represent an HTTP Endpoint. A target endpoint combines the protocol, a port, and the target URI. For instance, a target endpoint in this example is following: https://localhost:44399/Student/MyFunction3/

The output shows different endpoints in the application.

using Microsoft.AspNetCore.Mvc;
using System;

namespace MVCWebAppExample.Controllers
{
    public class StudentController : Controller
    {
        // GET: /Student/
        public String Index()
        {
            return "An Example of Returning Default";
        }
        // GET: /Student/MyFunction/
        public String MyFunction1()
        {
            String p = "Action 1 Performed!";
            return p;
        }
        public String MyFunction2()
        {
            String p = "Action 2 Performed!";
            return p;
        }
        public String MyFunction3()
        {
            String p = "Action 3 Performed!";
            return p;
        }
    }
}

Output

Example of Controller in an ASP.NET MVC Application
Example of Controller in an ASP.NET MVC Application

Summary

To sum up, this article briefly provides an Overview of MVC Architecture and the concept of Model, View, and Controller is also discussed. Further, an example of creating an MVC application in ASP.NET is also provided.


Related Topics

A Brief Overview of Laravel

Leave a Reply

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