Welcome to ASP.NET Core MVC
This tutorial will guide you through the fundamental concepts of building a web application using ASP.NET Core MVC. We'll cover the core components of the MVC pattern and show you how to create controllers, views, and models.
What is MVC?
The Model-View-Controller (MVC) architectural pattern separates an application into three interconnected components:
- Model: Represents the data and the business logic of the application.
- View: Responsible for presenting the data to the user, typically through HTML.
- Controller: Handles user input, interacts with the Model, and selects the appropriate View to render.
Let's Get Started
Follow these steps to create your first ASP.NET Core MVC application.
Install .NET SDK
Ensure you have the latest .NET SDK installed. You can download it from the official .NET website.
Create a New Project
Open your terminal or command prompt and run the following command to create a new MVC project:
dotnet new mvc -o MyFirstMvcApp
cd MyFirstMvcApp
This command creates a new directory named MyFirstMvcApp and sets up a basic ASP.NET Core MVC project structure.
Run the Application
Navigate to your project directory and run the application:
dotnet run
Open your web browser and go to https://localhost:5001 (or the port specified in your terminal) to see your running application.
Explore the Project Structure
Familiarize yourself with the generated files and folders:
- Controllers: Contains your controller classes (e.g.,
HomeController.cs). - Views: Contains your view files (
.cshtmlfiles), often organized into folders for each controller. - Models: For your application's data models.
- wwwroot: Contains static files like CSS, JavaScript, and images.
- Startup.cs: Configures the application's request pipeline and services.
Key Concepts
Understanding these core concepts will help you build more complex applications:
Routing
ASP.NET Core MVC uses a routing system to map incoming HTTP requests to specific controller actions. The default route is usually defined in Startup.cs:
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
This means a request like /Products/Details/123 will look for the Details action in the ProductsController with an ID of 123.
Controllers and Actions
Controllers are C# classes that inherit from Controller. Actions are public methods within a controller that handle specific requests.
// Controllers/HomeController.cs
public class HomeController : Controller
{
public IActionResult Index()
{
return View(); // Renders the Index.cshtml view
}
public IActionResult About()
{
ViewBag.Message = "Your application description page.";
return View(); // Renders the About.cshtml view
}
}
Views
Views are typically Razor files (.cshtml) that contain HTML markup mixed with C# code for dynamic content.
// Views/Home/Index.cshtml
@{
ViewData["Title"] = "Home Page";
}
Getting started
ASP.NET Core is an open-source framework for building web applications, IoT apps, and mobile backends.
Build apps that run anywhere
A unified platform for developers to create various types of apps—web, mobile, desktop, games, IoT, and cloud.
Develop with confidence
An agile, open-source, and community-focused framework for building modern web applications.
Models
Models represent your data. They can be simple C# classes.
// Models/Product.cs
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
Next Steps
You've successfully created and run your first ASP.NET Core MVC application. Now, you can:
- Create more controllers and actions.
- Define models and connect them to your views.
- Learn about data binding and validation.
- Explore Razor syntax and Tag Helpers.
- Integrate with databases using Entity Framework Core.
Continue your learning journey on the official ASP.NET Core documentation.
View Advanced Tutorials