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:
- .NET Core SDK (version 3.1 or later recommended)
- A code editor such as Visual Studio Code or Visual Studio.
Step 1: Create a New Project
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.
Using Visual Studio
- Open Visual Studio.
- Click "Create a new project".
- Search for "ASP.NET Core Web App (Model-View-Controller)".
- Select the template and click "Next".
- 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:
Controllers/
: Contains your application's controllers, which handle incoming requests and interact with models and views.Models/
: Contains your application's models, representing the data and business logic.Views/
: Contains your application's views, which are responsible for rendering the user interface.wwwroot/
: Contains static assets like CSS, JavaScript, and images.Program.cs
: The entry point of your application.Startup.cs
: Configures the application's services and request pipeline.appsettings.json
: Contains application configuration settings.
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:
Controllers
is the namespace for your controllers.HomeController
inherits fromController
, providing MVC capabilities.Index()
is an action method that returns aViewResult
, rendering theIndex.cshtml
view.Privacy()
does the same for the privacy page.Error()
handles error scenarios.
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.