Skip to main content

03 - ASP.NET Core

A framework for building web apps and services with .NET and C#.

ASP.NET Core does several web technologies. We are going to focus on two - serverside HTML (MVC and Razor Pages) and web services (Web API).

ASP.NET Core MVC

MVC is a design pattern that separates the application into 3 components:

  • Model - the data and business logic
  • View - the UI
  • Controller - the glue between the Model and View

Routing is the process of mapping a URL to a controller action. The routing engine parses the URL and matches it to a route. The route is then used to choose and execute the controller action.

Controller can have several actions. Each action can handle a different HTTP method (GET, POST, etc.) and different incoming url (route).

MVC

  • MVC pattern is from SmallTalk team, first academic paper is from year 1979
  • By the original definition
    • MODEL – business info and logic
    • VIEW – visual (partial) representation of model
    • CONTROLLER – middle part in between user and system
  • Derivatives: MVP, MVVM, ...
    • MVC separates into Model, View, and Controller
    • MVVM adds a ViewModel as an intermediary
    • MVP introduces a Presenter to mediate interactions
    • MVI adds Intent. Unidirectional data flow, immutable, reactive programming style

Modern web uses the same pattern – html as model, css as view and browser as controller

MVC Separation

MVC

Why MVC?

  • Separation of Concerns – business logic (model) and user interface (view) should be separated from each other. BL/Model represent our understanding of real world, UI is just for presentation and manipulation
  • Business logic should be totally independent
  • Business logic should be able to support several different UI-s
  • Controller intermediates between BL (Model) and UI

ASP.NET Core Razor Pages

Razor Pages is a page-focused framework for building web UI. It combines the features of MVC with the simplicity of Razor Pages. Here one html with code-behind file servers one url and supports all the HTTP methods (GET, POST, etc.).

ASP.NET Core Web API

Web API is a framework for building HTTP services. It is a lightweight framework that is used to build RESTful services. Uses the same MVC pattern as ASP.NET Core MVC html - but output is serialized data over http protocol - JSON, XML, etc.

Scaffolding

After designing your domain models, you use scaffolding to generate the controller and views for your models.

Tooling (update or install):

dotnet tool update -g dotnet-ef
dotnet tool update -g dotnet-aspnet-codegenerator

Install packages:

  • Microsoft.VisualStudio.Web.CodeGeneration.Design
  • Microsoft.EntityFrameworkCore.Design
  • Microsoft.EntityFrameworkCore.SqlServer

Razor pages

dotnet aspnet-codegenerator razorpage \
-m <MODEL> \
-outDir Pages/<MODEL in plural> \
-dc AppDbContext \
--useDefaultLayout \
--referenceScriptLibraries \
-f

MVC

dotnet aspnet-codegenerator controller \
-name <MODEL in plural>Controller \
-m <MODEL> \
-actions \
-dc AppDbContext \
-outDir Areas/Admin/Controllers \
--useDefaultLayout \
--useAsyncActions \
--referenceScriptLibraries \
-f

Web API

dotnet aspnet-codegenerator controller \
-name <MODEL in plural>Controller \
-m <MODEL> \
-actions \
-dc AppDbContext \
-outDir ApiControllers \
-api \
--useAsyncActions \
-f

Initially use dbcontext in controllers, later we will swap it out with layered architecture - DAL, BLL, etc. (CLEAN/ONION architecture, then modular monolith, then microservices - architecture is the MAIN focus of this course).

What to Know for Code Defense

Be prepared to explain:

  1. Why MVC as a pattern? — MVC separates business logic (Model), presentation (View), and user interaction handling (Controller). This separation lets you change the UI without touching business logic, and vice versa.
  2. What is the difference between MVC, Razor Pages, and Web API in ASP.NET Core? — MVC uses controllers with multiple actions and separate view files. Razor Pages combine a single page with its code-behind for simpler page-focused scenarios. Web API uses the same MVC pattern but returns serialized data instead of HTML.
  3. Why use scaffolding, and what are its limitations? — Scaffolding generates boilerplate CRUD controllers and views quickly. However, scaffolded code puts data access directly in controllers. You must refactor toward layered architecture as the project grows.
  4. Why start with DbContext in controllers and swap to layered architecture later? — Starting simple lets you learn the framework mechanics without the overhead of abstractions. Once you understand how controllers, model binding, and EF Core work together, adding layers makes sense.
  5. What are the derivatives of MVC (MVP, MVVM, MVI) and when do they apply? — MVP uses a Presenter for mediation (common in desktop). MVVM adds a ViewModel with two-way data binding (WPF, mobile). MVI uses unidirectional data flow with immutable state. The choice depends on the UI framework.