ASP.NET Core Fundamentals
This section provides an in-depth overview of the core concepts and architectural patterns of ASP.NET Core, the cross-platform, high-performance, open-source framework for building modern, cloud-enabled applications.
What is ASP.NET Core?
ASP.NET Core is a rewrite of ASP.NET that is modular, fast, and designed for modern development practices. It allows you to build:
- Web applications
- Web APIs
- Microservices
- Real-time applications (e.g., with SignalR)
Key benefits include:
- Cross-platform: Run on Windows, macOS, and Linux.
- High performance: Optimized for speed and efficiency.
- Open-source and community-driven: Developed in the open on GitHub.
- Unified story for web UI and web API: Build both using a single programming model.
- Dependency Injection: Built-in support for managing dependencies.
- Middleware pipeline: Highly configurable request processing pipeline.
Core Concepts
Middleware Pipeline
ASP.NET Core uses a middleware pipeline to handle incoming HTTP requests. Each piece of middleware can:
- Execute logic before the next middleware.
- Short-circuit the request pipeline.
- Call the next middleware in the pipeline.
- Modify the request and response objects.
Common middleware includes:
UseRouting(): For routing requests to the appropriate endpoint.UseAuthentication(): For handling authentication.UseAuthorization(): For handling authorization.UseEndpoints(): For executing the endpoint delegate.
Example of configuring the pipeline in Program.cs:
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorPages();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapRazorPages();
app.Run();
Dependency Injection (DI)
ASP.NET Core has a built-in, lightweight DI container. DI is a design pattern that promotes loosely coupled code by providing dependencies to objects rather than having objects create their own dependencies.
You register services in the DI container and inject them into your application's classes (controllers, services, etc.).
Example:
// In Program.cs (or Startup.cs for older versions)
builder.Services.AddTransient<IMyService, MyService>();
// In a controller
public class HomeController : Controller
{
private readonly IMyService _myService;
public HomeController(IMyService myService)
{
_myService = myService;
}
public IActionResult Index()
{
ViewBag.Message = _myService.GetData();
return View();
}
}
Configuration
ASP.NET Core provides a flexible configuration system that allows you to load settings from various sources like:
- JSON configuration files (appsettings.json)
- Environment variables
- Command-line arguments
- Azure Key Vault
The IConfiguration interface provides access to these settings.
Hosting Model
ASP.NET Core applications are hosted in a WebHost. The WebHost is responsible for:
- Starting the web server (Kestrel by default).
- Configuring the application's middleware pipeline.
- Managing the application's lifecycle.
In modern ASP.NET Core (from .NET 6 onwards), the hosting model is simplified using the WebApplicationBuilder and WebApplication classes in Program.cs.
Next Steps
Now that you understand the fundamentals, explore how to build specific types of web applications: