Creating Your First ASP.NET Core MVC Application

This guide will walk you through the process of building a simple ASP.NET Core MVC application from scratch. We'll cover setting up your development environment, creating the project, and understanding the basic structure.

Prerequisites

Before you begin, ensure you have the following installed:

Step 1: Create a New Project

1

Using the .NET CLI

Open your terminal or command prompt and navigate to the directory where you want to create your project. Then, run the following command:

dotnet new mvc -o MyFirstMvcApp

This command creates a new directory named MyFirstMvcApp and generates a basic ASP.NET Core MVC project within it.

2

Using Visual Studio

  1. Open Visual Studio.
  2. Click "Create a new project".
  3. Search for "ASP.NET Core Web App (Model-View-Controller)".
  4. Select the template and click "Next".
  5. Enter your project name (e.g., MyFirstMvcApp) and location, then click "Create".

Step 2: Explore the Project Structure

Navigate into the newly created project directory (MyFirstMvcApp). You'll find the following key folders and files:

Note: In newer .NET versions (like .NET 6 and later), the startup configuration is often integrated into Program.cs, simplifying the project structure.

Step 3: Running Your Application

To see your application in action, run the following command in your project directory:

dotnet run

Once the application is running, you'll see an output indicating the URLs your application is listening on (typically https://localhost:5001 and http://localhost:5000). Open your web browser and navigate to one of these URLs to view your default page.

Step 4: Understanding a Basic MVC Component (Example: The HomeController)

Let's examine a fundamental part of the generated application:

Controllers/HomeController.cs

This controller handles requests for the home page:

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

namespace MyFirstMvcApp.Controllers
{
    public class HomeController : Controller
    {
        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 });
        }
    }
}

Here:

Views/Home/Index.cshtml

This is the view rendered by the Index() action:

@{
    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>

This Razor view uses HTML and C# syntax to dynamically generate content.

Conclusion

You have successfully created and explored your first ASP.NET Core MVC application! This foundation allows you to build more complex web applications with a structured and maintainable approach.

Continue exploring the official ASP.NET Core MVC documentation for advanced topics.