MVC

What Is MVC Architecture in ASP.NET Core? Model, View, Controller Explained with Examples (2026)

Introduction

MVC stands for Model–View–Controller. It is a software architectural pattern used to separate an application into three interconnected components: Model, View, and Controller.

MVC is widely used in web application development because it helps organize application code, separate responsibilities, and improve maintainability and testability.

In ASP.NET Core MVC, developers use C# to implement controllers and application logic, Razor views to generate HTML, and models to represent application data and related behavior.

In this tutorial, you will learn what MVC is, how its components work, how an HTTP request is processed, and how to create a simple ASP.NET Core MVC application using C#.

1. What Is MVC Architecture?

MVC is an architectural pattern that divides an application into three components:

  • Model: Represents application data, state, and related rules.

  • View: Defines how information is presented to the user.

  • Controller: Handles incoming requests, coordinates application operations, and selects the appropriate response.

The main objective of MVC is separation of concerns.

Instead of combining data handling, user interface markup, and request processing in one file, MVC organizes these responsibilities into separate components.

Uploading: 60092 of 60092 bytes uploaded.


2. Model in MVC

The Model represents application data and related state or rules.

In ASP.NET Core MVC, models commonly include classes used for representing database entities, form inputs, and information displayed in views.

Models do not necessarily contain all business logic. In larger applications, business rules are often implemented in dedicated services or domain components.

Example: Employee Model

Create a file named Employee.cs inside the Models folder.

using System.ComponentModel.DataAnnotations;

namespace MvcDemo.Models
{
    public class Employee
    {
        public int Id { get; set; }

        [Required]
        public string Name { get; set; } = string.Empty;

        [Range(0, 1000000)]
        public decimal Salary { get; set; }
    }
}

Explanation

  • Id represents the employee identifier.

  • Name stores the employee's name.

  • Salary stores the employee's salary.

  • [Required] specifies a validation requirement.

  • [Range] defines an allowed salary range.

This model can be used to transfer data between controllers and views or as part of an application's data access layer.

3. View in MVC

The View is responsible for presenting information to the user.

In ASP.NET Core MVC, views are commonly written using Razor syntax and stored in .cshtml files.

A view can contain HTML, CSS classes, and Razor expressions.

Example: Index.cshtml

Create a file inside:

Views/Employee/Index.cshtml

Add the following code:

@model MvcDemo.Models.Employee

<h2>Employee Details</h2>

<p>Employee ID: @Model.Id</p>

<p>Employee Name: @Model.Name</p>

<p>Salary: @Model.Salary</p>

Explanation

The @model directive declares the type of data expected by the view.

Razor expressions such as @Model.Name display values provided by the controller.

Views should generally focus on presentation rather than complex business logic or direct database operations.

4. Controller in MVC

A Controller handles incoming HTTP requests and coordinates the application's response.

Controllers commonly:

  1. Receive requests through routing.

  2. Accept and validate request input.

  3. Call application services or other components.

  4. Prepare data for presentation.

  5. Return a view, JSON response, redirect, or another action result.

Example: EmployeeController.cs

Create a controller inside the Controllers folder.

using Microsoft.AspNetCore.Mvc;
using MvcDemo.Models;

namespace MvcDemo.Controllers
{
    public class EmployeeController : Controller
    {
        public IActionResult Index()
        {
            var employee = new Employee
            {
                Id = 101,
                Name = "Rahul",
                Salary = 45000
            };

            return View(employee);
        }
    }
}

Explanation

The EmployeeController class inherits from the ASP.NET Core MVC Controller base class.

The Index() action creates an Employee object and passes it to the corresponding Razor view.

The following statement returns the view:

return View(employee);

By convention, MVC looks for the view at:

Views/Employee/Index.cshtml

5. How MVC Architecture Works

The following diagram illustrates a typical ASP.NET Core MVC request flow.

           User / Browser
                 |
                 | HTTP Request
                 v
          ASP.NET Core Routing
                 |
                 v
             Controller
                 |
                 | Calls Services / Models
                 v
       Application and Data Layer
                 |
                 | Returns Data
                 v
             Controller
                 |
                 | Passes Model
                 v
                View
                 |
                 | Generates HTML
                 v
           HTTP Response
                 |
                 v
           User / Browser

Step-by-Step MVC Request Flow

Step 1: The user enters a URL in the browser.

https://localhost:5001/Employee/Index

The port number is an example and depends on the application's configuration.

Step 2: ASP.NET Core routing matches the request to an endpoint.

Step 3: The EmployeeController's Index action executes.

Step 4: The controller creates or retrieves employee data.

Step 5: The controller passes the employee model to the view.

Step 6: The Razor view generates HTML.

Step 7: ASP.NET Core returns the HTTP response to the browser.

6. ASP.NET Core MVC Project Structure

A typical ASP.NET Core MVC application contains the following folders and files:

MvcDemo/
│
├── Controllers/
│   ├── HomeController.cs
│   └── EmployeeController.cs
│
├── Models/
│   └── Employee.cs
│
├── Views/
│   ├── Employee/
│   │   └── Index.cshtml
│   └── Shared/
│       └── _Layout.cshtml
│
├── wwwroot/
│   ├── css/
│   │   └── site.css
│   └── js/
│       └── site.js
│
├── appsettings.json
├── Program.cs
└── MvcDemo.csproj

Important Folders

FolderPurpose
ControllersContains MVC controllers
ModelsContains application and view models
ViewsContains Razor view files
wwwrootStores publicly served static files
Services (optional)Contains application services
Data (optional)Contains database access components

The project structure can vary depending on application architecture and developer preferences.

7. Configure MVC in ASP.NET Core

ASP.NET Core applications typically configure services and the HTTP request pipeline in Program.cs.

Example: Program.cs

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddControllersWithViews();

var app = builder.Build();

if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
    app.UseHsts();
}

app.UseHttpsRedirection();

app.UseRouting();

app.UseAuthorization();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

app.Run();

Explanation

AddControllersWithViews() registers the services needed for MVC controllers and Razor views.

MapControllerRoute() configures a conventional route pattern.

For example:

/Employee/Index/101

can map to the Employee controller, Index action, and an optional ID value, provided the action and route configuration support it.

8. Advantages of MVC Architecture

Separation of Concerns

MVC separates presentation, request handling, and application data responsibilities.

Maintainability

Developers can modify views without unnecessarily changing controllers or data models.

Testability

Controllers and application services can be tested independently when designed with suitable dependencies.

Code Organization

MVC provides clear conventions for organizing files and application components.

Team Collaboration

Frontend and backend developers can work on different parts of the application.

Reusability

Models, services, layouts, and reusable view components can be shared across application features.

9. MVC vs Traditional ASP.NET Web Forms

FeatureASP.NET MVC / Core MVCASP.NET Web Forms
ArchitectureModel–View–ControllerPage and control event model
View files.cshtml.aspx
Request handlingController actionsPage lifecycle and control events
State managementExplicit application and request stateIncludes ViewState
HTML renderingRazor views and HTMLWeb Forms server controls
URL handlingEndpoint routingPage URLs and routing
ASP.NET Core supportYesNo

ASP.NET Web Forms is supported on the traditional .NET Framework, while ASP.NET Core MVC is designed for modern .NET applications.

10. MVC vs Web API

ASP.NET Core MVC and ASP.NET Core Web API share much of the same underlying framework, including routing, controllers, model binding, and dependency injection.

However, their typical purposes differ.

FeatureMVCWeb API
Primary purposeRender web interfacesExpose HTTP APIs
Typical responseHTMLJSON or other formats
View supportRazor viewsUsually not required
Common base classControllerControllerBase
Common useServer-rendered websitesBackend services, mobile and SPA APIs

An ASP.NET Core application can support both MVC views and API endpoints.

11. Common MVC Interview Questions

What is the full form of MVC?

MVC stands for Model–View–Controller.

What is the role of the Model in MVC?

The Model represents application data, state, and related rules.

What is the role of the View?

The View presents information to users, commonly by rendering HTML through Razor.

What is the role of the Controller?

The Controller handles incoming requests, coordinates application operations, and returns the appropriate response.

What is Razor in ASP.NET Core MVC?

Razor is a markup syntax that allows C# expressions and code to be embedded in HTML templates.

What is routing in MVC?

Routing matches incoming HTTP requests to application endpoints, including controller actions.

What is the difference between ViewBag, ViewData, and TempData?

  • ViewBag: A dynamic wrapper around ViewData.

  • ViewData: A dictionary used to pass data between a controller and view during the current request.

  • TempData: Stores data intended to be available across requests, commonly used with redirects.

Can a Controller return JSON instead of a View?

Yes. A controller action can return JSON or other HTTP responses instead of rendering a Razor view.

Does MVC require Entity Framework Core?

No. MVC is independent of the database access technology. Developers can use Entity Framework Core, Dapper, ADO.NET, external APIs, or other data sources.

Conclusion

MVC is an important architectural pattern for building organized and maintainable web applications.

The three main components have distinct responsibilities:

Model: Represents application data and related rules.

View: Displays information to the user.

Controller: Handles incoming requests and coordinates the application's response.

ASP.NET Core MVC combines this architecture with modern .NET features such as dependency injection, endpoint routing, Razor views, and middleware.

Understanding MVC architecture is an essential foundation for developers learning ASP.NET Core web development.



Official Microsoft Documentation


Comments