ASP.NET Core MVC

ASP.NET Core MVC is a high-performance, cross-platform framework for building modern, cloud-enabled, internet-connected applications. It's a lightweight, open-source, and extensible framework based on the Model-View-Controller (MVC) design pattern.

What is MVC?

The Model-View-Controller (MVC) architectural pattern separates an application into three interconnected components:

Key Features of ASP.NET Core MVC

Getting Started

To create an ASP.NET Core MVC application, you can use the .NET CLI:

dotnet new mvc -o MyMvcApp
cd MyMvcApp
dotnet run

This will create a new MVC project and start a development server. You can then access your application at the provided URL (usually https://localhost:5001 or http://localhost:5000).

Core Components Explained

Controllers

Controllers are C# classes that inherit from Microsoft.AspNetCore.Mvc.Controller. They contain action methods that handle incoming requests.

public class HomeController : Controller
{
    public IActionResult Index()
    {
        return View(); // Returns the Index.cshtml view
    }

    public IActionResult About()
    {
        ViewData["Message"] = "Your application description page.";
        return View();
    }

    public IActionResult Contact()
    {
        ViewData["Message"] = "Your contact page.";
        return View();
    }
}

Views

Views are typically implemented using Razor syntax (.cshtml files). They render the HTML that is sent to the client.

@model YourApp.Models.HomeViewModel

@{
    ViewData["Title"] = "Home Page";
}

Welcome to ASP.NET Core MVC!

@Model.Message

Learn more »

Models

Models represent the data structure and business logic. They can be simple POCOs (Plain Old C# Objects).

namespace YourApp.Models
{
    public class HomeViewModel
    {
        public string Message { get; set; }
    }
}

Further Reading

Commonly Used APIs

Controller

Base class for controllers. Provides access to properties like Request, Response, ViewData, and methods for returning results.

Key methods: View(), RedirectToAction(), NotFound(), Ok().

IActionResult

Represents an action result that can be executed by the action method, typically by returning an instance of a class that implements this interface.

Common implementations: ViewResult, RedirectResult, StatusCodeResult, JsonResult.

ViewDataDictionary

A dictionary that allows you to pass data from the controller to the view. It's a simple key-value store.

ModelStateDictionary

Stores model state, including validation errors and bound values.