MSDN Documentation

ASP.NET Core Fundamentals

This section provides a deep dive into the core concepts and building blocks of ASP.NET Core, a modern, cross-platform, and high-performance framework for building web applications and APIs.

Introduction to ASP.NET Core

ASP.NET Core is an open-source, cross-platform framework developed by Microsoft. It's a re-imagining of ASP.NET that offers significant performance improvements, a modular architecture, and flexibility for building various types of web applications.

Key benefits include:

Architecture Overview

ASP.NET Core applications are built around a core set of components:

The application's lifecycle is managed by the host, which initializes and manages the application's resources.

Middleware Pipeline

ASP.NET Core uses a middleware pipeline to process HTTP requests. Each piece of middleware in the pipeline has the opportunity to:

Common middleware includes:

The pipeline is configured in the Configure method of the Startup class.


public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    else
    {
        app.UseExceptionHandler("/Error");
        app.UseHsts();
    }

    app.UseHttpsRedirection();
    app.UseStaticFiles();
    app.UseRouting();
    app.UseAuthentication();
    app.UseAuthorization();

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapRazorPages();
        endpoints.MapControllerRoute(
            name: "default",
            pattern: "{controller=Home}/{action=Index}/{id?}");
    });
}
            

Dependency Injection

ASP.NET Core has built-in support for Dependency Injection (DI), a design pattern that promotes loose coupling and testability. Services are registered with a DI container, and then injected into classes that require them.

Services can be registered with different lifetimes:

Registration typically occurs in the ConfigureServices method of the Startup class.


public void ConfigureServices(IServiceCollection services)
{
    services.AddRazorPages();
    services.AddTransient(); // Example of transient registration
}
            

Configuration

ASP.NET Core's configuration system is flexible and supports various sources, including JSON files, environment variables, command-line arguments, and more. This allows applications to be configured differently for development, staging, and production environments.

The IConfiguration interface provides access to configuration values.

Example appsettings.json:


{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*"
}
            

Routing

Routing determines how ASP.NET Core applications respond to HTTP requests. It maps incoming requests to the correct handler or action.

ASP.NET Core supports two primary routing mechanisms:

The UseRouting() and UseEndpoints() middleware are crucial for routing.

Razor Pages

Razor Pages provide a page-centric model for building web UI with ASP.NET Core. Each page consists of a Razor file (.cshtml) and a code-behind file (.cshtml.cs) that handles page logic, data binding, and event handlers.

Razor Pages are suitable for:

Model-View-Controller (MVC)

The Model-View-Controller (MVC) architectural pattern is a core option for building complex web applications in ASP.NET Core. It separates concerns into three interconnected components:

Building Web APIs

ASP.NET Core excels at building high-performance Web APIs. You can create RESTful services using either the MVC pattern (with controllers) or Minimal APIs for simpler scenarios.

Key features for API development include:

Hosting and Deployment

ASP.NET Core applications can be hosted in various ways:

Deployment options include publishing to Azure, Docker containers, Windows Services, or Linux daemons.