ASP.NET Core Fundamentals

Introduction to ASP.NET Core

ASP.NET Core is a cross-platform, high-performance, open-source framework for building modern, cloud-based, Internet-connected applications. It is a rewrite of the ASP.NET platform and has been redesigned from the ground up to address the limitations of previous versions and to meet the demands of modern web development.

Key characteristics of ASP.NET Core include:

  • Cross-platform: Runs on Windows, macOS, and Linux.
  • High-performance: Built for speed and efficiency.
  • Open-source: Community-driven development with a transparent roadmap.
  • Modular: Composed of a set of NuGet packages, allowing you to include only the dependencies you need.
  • Cloud-ready: Optimized for cloud deployment and microservices.
  • Unified story: Supports building Web APIs, MVC, Razor Pages, and Blazor applications.

Core Concepts

Request Pipeline

The ASP.NET Core request pipeline is a series of middleware components that process HTTP requests and responses. Each middleware component can perform specific tasks, such as routing, authentication, authorization, and error handling.

Here's a simplified view of the pipeline:

  1. Request arrives.
  2. Middleware components process the request sequentially.
  3. The pipeline can terminate early (e.g., by returning a response).
  4. If the request reaches the end, a response is generated and sent back.

Middleware

Middleware is a piece of software that is designed to be incorporated into an application processing pipeline. It has access to the request and response objects and can perform actions, call the next middleware in the pipeline, or terminate the pipeline.


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.UseAuthorization();

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapRazorPages();
        // Add other endpoint mappings here
    });
}
                        

Project Structure

A typical ASP.NET Core project has a well-defined structure to organize code and assets:

  • Pages/ (for Razor Pages): Contains Razor Page files (.cshtml) and their code-behind files (.cshtml.cs).
  • Controllers/ (for MVC): Contains controller classes that handle requests.
  • wwwroot/: Contains static files like HTML, CSS, JavaScript, and images.
  • appsettings.json: Configuration file for application settings.
  • Program.cs: The entry point of the application, where the web host is configured and started.
  • Startup.cs: (In older versions, now integrated into Program.cs) Configures the application's services and request pipeline.

Dependency Injection

ASP.NET Core has built-in support for dependency injection (DI), a design pattern that promotes loose coupling and makes applications more maintainable and testable. Services are registered in the DI container, and dependencies are injected into classes through their constructors.


// In Startup.cs (or Program.cs for .NET 6+)
public void ConfigureServices(IServiceCollection services)
{
    services.AddTransient<IMyService, MyService>();
    services.AddRazorPages();
}

// In a Razor Page or Controller
public class MyPageModel : PageModel
{
    private readonly IMyService _myService;

    public MyPageModel(IMyService myService)
    {
        _myService = myService;
    }

    public void OnGet()
    {
        ViewData["Message"] = _myService.GetMessage();
    }
}