ASP.NET Core MVC Basics

Welcome to the fundamentals of ASP.NET Core MVC. This module will introduce you to the core concepts of the Model-View-Controller (MVC) architectural pattern and how it's implemented in ASP.NET Core.

What is MVC?

Model-View-Controller (MVC) is an architectural pattern for implementing user interfaces. It divides an application into three interconnected components:

The ASP.NET Core MVC Request Lifecycle

When a request comes into an ASP.NET Core MVC application, it follows a specific lifecycle:

  1. Request received: The web server (e.g., Kestrel) receives an HTTP request.
  2. Routing: The routing middleware analyzes the request URL and determines which controller and action method should handle the request based on defined routes.
  3. Controller Execution: The identified controller's action method is executed. This might involve interacting with the Model to retrieve or manipulate data.
  4. View Selection: After the action method completes, it typically returns a result, often a ViewResult, which specifies which View to render.
  5. View Rendering: The selected View (usually a Razor file, .cshtml) is processed. It uses the data passed from the controller (often via a ViewModel) to generate HTML.
  6. Response Sent: The generated HTML is sent back to the client as an HTTP response.

Key Components

Controllers

Controllers are C# classes that inherit from Controller. They contain public methods called "action methods" which are executed in response to specific requests.


using Microsoft.AspNetCore.Mvc;

public class HomeController : Controller
{
    public IActionResult Index()
    {
        ViewBag.Message = "Welcome to ASP.NET Core MVC!";
        return View(); // Renders the Index.cshtml view
    }

    public IActionResult About()
    {
        return View(); // Renders the About.cshtml view
    }
}
            

Views

Views are typically Razor files (.cshtml) that contain a mix of HTML and C# code. They are responsible for the user interface.


@* Views/Home/Index.cshtml *@
@{
    ViewData["Title"] = "Home Page";
}

@ViewBag.Message

This is a sample ASP.NET Core MVC application.

Getting started

ASP.NET Core MVC provides a robust framework for building web applications.

Learn more »

Build tools

Use Visual Studio or VS Code to develop your applications.

Learn more »

Run & deploy

Deploy your application to Azure or other hosting platforms.

Learn more »

Models

Models represent your application's data. They can be simple POCO (Plain Old CLR Object) classes or more complex objects that interact with a database.


public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
}
            

Tip:

For passing data from the controller to the view, it's best practice to use strongly-typed ViewModels instead of ViewBag or ViewData for better type safety and maintainability.

Razor Syntax

Razor is a templating engine that allows you to embed server-side code within HTML. Key Razor syntax includes:

Important:

Understanding the MVC pattern is crucial for building scalable and maintainable web applications with ASP.NET Core. Each component plays a distinct role, ensuring separation of concerns.