ASP.NET Core Basics
This document provides an introduction to the fundamental concepts of ASP.NET Core, a cross-platform, high-performance, open-source framework for building modern, cloud-based, internet-connected applications.
What is ASP.NET Core?
ASP.NET Core is a re-architected version of ASP.NET that:
- Is cross-platform: Runs on Windows, macOS, and Linux.
- Is open-source and community-focused.
- Supports various application models: Web UI, web APIs, real-time apps, microservices, and more.
- Has high performance and is designed for modern web development.
- Is modular, allowing you to include only the components you need.
Key Concepts
Project Structure
A typical ASP.NET Core project includes the following key files and folders:
Program.cs
: The entry point of the application. It defines the host builder and configures the application's services and request pipeline.Startup.cs
(in older .NET versions, now integrated intoProgram.cs
): Contains methods to configure the application's services (ConfigureServices
) and the HTTP request pipeline (Configure
).appsettings.json
: Configuration file for application settings.wwwroot
folder: Contains static files like CSS, JavaScript, and images.Controllers
folder (for MVC): Contains controllers that handle incoming requests.Pages
folder (for Razor Pages): Contains Razor Page files.
The Request Pipeline
ASP.NET Core uses a middleware pipeline to handle HTTP requests. Middleware components are executed in a specific order to process requests and responses. Common middleware includes:
- Static Files Middleware: Serves static files.
- Routing Middleware: Matches incoming requests to endpoints.
- Authentication Middleware: Handles user authentication.
- Authorization Middleware: Enforces authorization rules.
- MVC or Razor Pages Middleware: Handles request processing for MVC or Razor Pages.
Dependency Injection
ASP.NET Core has built-in support for Dependency Injection (DI), making it easier to manage dependencies and write loosely coupled code. Services are registered with the DI container and can be injected into controllers, Razor Pages, or other services.
// Example of registering a service in Program.cs
builder.Services.AddScoped();
Configuration
ASP.NET Core supports multiple configuration sources, including JSON files, environment variables, command-line arguments, and Azure Key Vault. This allows you to manage application settings effectively across different environments.
appsettings.json
for development and environment-specific configuration files (e.g., appsettings.Development.json
) for overrides.
Getting Started
To start building an ASP.NET Core application, you typically need:
- The .NET SDK installed on your machine.
- A code editor like Visual Studio, VS Code, or JetBrains Rider.
- Create a new project using the .NET CLI:
dotnet new webapp -o MyAspNetCoreApp
cd MyAspNetCoreApp
dotnet run
This will create a basic web application and run it, allowing you to explore the project structure and begin developing your application.
Next Steps
Explore the following topics to deepen your understanding of ASP.NET Core: