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.

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
Idrepresents the employee identifier.Namestores the employee's name.Salarystores 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.cshtmlAdd 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:
Receive requests through routing.
Accept and validate request input.
Call application services or other components.
Prepare data for presentation.
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.cshtml5. 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 / BrowserStep-by-Step MVC Request Flow
Step 1: The user enters a URL in the browser.
https://localhost:5001/Employee/IndexThe 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.csprojImportant Folders
| Folder | Purpose |
|---|---|
| Controllers | Contains MVC controllers |
| Models | Contains application and view models |
| Views | Contains Razor view files |
| wwwroot | Stores 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/101can 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
| Feature | ASP.NET MVC / Core MVC | ASP.NET Web Forms |
|---|---|---|
| Architecture | Model–View–Controller | Page and control event model |
| View files | .cshtml | .aspx |
| Request handling | Controller actions | Page lifecycle and control events |
| State management | Explicit application and request state | Includes ViewState |
| HTML rendering | Razor views and HTML | Web Forms server controls |
| URL handling | Endpoint routing | Page URLs and routing |
| ASP.NET Core support | Yes | No |
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.
| Feature | MVC | Web API |
|---|---|---|
| Primary purpose | Render web interfaces | Expose HTTP APIs |
| Typical response | HTML | JSON or other formats |
| View support | Razor views | Usually not required |
| Common base class | Controller | ControllerBase |
| Common use | Server-rendered websites | Backend 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.

Comments
Post a Comment