Basic ASP.NET Core MVC Application Sample

Welcome to the documentation for the basic ASP.NET Core MVC sample application. This guide will walk you through the core components and how to set up and run this simple project.

Introduction

This sample application demonstrates the fundamental principles of the Model-View-Controller (MVC) architectural pattern within the ASP.NET Core framework. MVC is a widely adopted pattern for building web applications that separates concerns into three interconnected parts:

This application is designed to be a starting point for developers new to ASP.NET Core MVC, providing a clear and concise example of how these components work together.

Project Setup

To create a similar project, you would typically use the .NET CLI or Visual Studio:

dotnet new mvc -o BasicMvcApp
cd BasicMvcApp

This command creates a new MVC project directory named BasicMvcApp and navigates into it.

Controllers

Controllers are C# classes that handle incoming browser requests. They typically inherit from Controller and contain public methods called actions. Each action method is responsible for handling a specific request and returning a result, usually a ViewResult that renders a specific view.

Example: Controllers/HomeController.cs

using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
using BasicMvcApp.Models;

namespace BasicMvcApp.Controllers
{
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;

        public HomeController(ILogger<HomeController> logger)
        {
            _logger = logger;
        }

        public IActionResult Index()
        {
            return View();
        }

        public IActionResult Privacy()
        {
            return View();
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }
}

Views

Views are responsible for generating the HTML response that is sent to the browser. They are typically implemented using Razor syntax, which allows embedding C# code directly into HTML.

The structure of views usually follows a convention: Views/[ControllerName]/[ActionName].cshtml.

Example: Views/Home/Index.cshtml

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

<div class="text-center">
    <h1 class="display-4">Welcome</h1>
    <p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
</div>
Tip: Razor syntax uses the @ symbol to transition from HTML to C#.

Models

Models represent the application's data. In this sample, a simple model ErrorViewModel is used to pass error information to the error view.

Example: Models/ErrorViewModel.cs

using System;

namespace BasicMvcApp.Models
{
    public class ErrorViewModel
    {
        public string? RequestId { get; set; }

        public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
    }
}

Routing

Routing maps incoming URL requests to specific controller actions. ASP.NET Core uses a convention-based router. By default, requests are routed to the Index action of the Home controller.

Configuration is typically found in Program.cs (or Startup.cs in older .NET versions).

// In Program.cs for .NET 6+
app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

Running the App

To run the application from the project directory, use the .NET CLI:

dotnet run

The application will typically start on https://localhost:7xxx (for HTTPS) and http://localhost:5xxx (for HTTP). You can open these URLs in your web browser.

Further Steps

This is a very basic example. To build more complex applications, explore topics like:

Refer to the official Microsoft documentation for comprehensive guides and API references.