ASP.NET Core Documentation

repository·main·Indexed 11 days ago

https://github.com/dotnet/aspnetcore.docs

Official documentation for ASP.NET Core, featuring guides, tutorials, and API references for building web applications. Includes comprehensive samples for Entity Framework Core migrations, session state, background tasks using IHostedService, high-performance logging with LoggerMessage, and middleware extensibility.

Tokens
482.5K
Snippets
1.4K
Records
2.1K
Agent score
95%

What's inside ASP.NET Core

  1. Overview of ASP.NET Core Middleware Extensibility Sample

    main

    This sample demonstrates different ways to activate middleware in ASP.NET Core, specifically focusing on scenarios described in 'Factory-based middleware activation'. It compares conventional middleware activation against activation via the IMiddleware interface.

    The sample implements middleware that:

    1. Reads a value from a query string parameter named key.
    2. Uses an injected database context (a scoped service) to record that value in an in-memory database.

    This demonstrates how middleware can interact with scoped services through different activation patterns.

  2. Overview of ASP.NET Core Data Protection

    main

    ASP.NET Core provides a cryptographic API designed to protect sensitive data, including built-in key management and automatic key rotation. It is specifically designed for web scenarios where you need to persist trusted information that might be passed through an untrusted client (e.g., authentication cookies or bearer tokens).

    Core Requirements Addressed:

    • Authenticity, Integrity, and Tamper-proofing: Ensures that data (like a permission token) hasn't been forged or modified by a client.
    • Confidentiality: Protects sensitive information (like file paths or server-specific data) from being disclosed to untrusted clients.
    • Isolation: Allows different components (e.g., an anti-CSRF mechanism and a bearer token component) to use the same stack without interfering with each other.

    Design Principles:

    • Zero Configuration: Strives to work out-of-the-box with minimal setup.
    • Safe APIs: Provides straightforward consumer APIs that are difficult to use incorrectly.
    • Automated Key Management: Handles algorithm selection and key lifetimes automatically so developers don't need to manage raw key material.
    • Automatic Protection: Automatically determines and applies appropriate protection mechanisms for keys at rest.
  3. Overview of HTTP.sys for ASP.NET Core

    main

    HTTP.sys is a Windows-only web server for ASP.NET Core that serves as an alternative to Kestrel. It is a mature, full-featured web server that provides robustness and security, often used when exposing a server directly to the internet without IIS or when specific features not available in Kestrel are required.

    Key Features:

    • Windows Authentication
    • Port sharing
    • HTTPS with SNI
    • HTTP/2 over TLS (Windows 10 or later)
    • Direct file transmission
    • Response caching
    • WebSockets (Windows 8 or later)

    Important Compatibility Notes:

    • Not compatible with the ASP.NET Core Module.
    • Cannot be used with IIS or IIS Express.
    • Supported Windows Versions: Windows 7 or later; Windows Server 2008 R2 or later.
  4. Options for NSwag code generation

    main

    NSwag provides several ways to generate API client code (such as C# or TypeScript) from a Swagger/OpenAPI specification:

    • NSwagStudio: A Windows desktop application for visual code generation.
    • NuGet Packages: Use NSwag.CodeGeneration.CSharp or NSwag.CodeGeneration.TypeScript to perform generation directly within your project.
    • Command Line: Use the NSwag CLI tool.
    • MSBuild: Use the NSwag.MSBuild NuGet package to integrate generation into your build process.
    • Unchase OpenAPI (Swagger) Connected Service: A Visual Studio extension that generates C# or TypeScript clients and can also generate C# controllers for OpenAPI services.
  5. ASP.NET Core URL Rewriting Sample Overview

    main

    This sample demonstrates how to use the ASP.NET Core URL rewriting middleware to implement URL redirects and URL rewriting rules.

    When a rule is applied:

    • Redirects: The client receives a redirect response (e.g., 301 or 302) and is instructed to request a different URL.
    • Rewrites: The request URL is changed internally. The client continues to request the original URL, but the middleware processes the request using the rewritten path. For static files, the Static file middleware serves the file based on the rewritten path.
  6. Response compression sample application (ASP.NET Core 2.x)

    main
    This sample demonstrates how to use ASP.NET Core 2.x response compression middleware to compress HTTP responses. It covers the implementation of Gzip and Brotli compression providers, as well as how to implement a custom compression provider. The sample also shows how to handle different MIME types (such as text/plain and image/svg+xml) and how to add new MIME types to the compression configuration.
  7. What is YARP Direct Forwarding and IHttpForwarder?

    main

    YARP Direct Forwarding is a lightweight proxying capability for applications that only need to forward specific requests to specific destinations without the overhead of full reverse proxy features like automatic routing, load balancing, or configuration discovery.

    At the center of this is IHttpForwarder, which acts as the core proxy adapter. It handles the mechanics of converting an incoming HttpContext into an outgoing HttpRequestMessage, sending it via an HttpMessageInvoker, and relaying the response back to the client.

    Capabilities of IHttpForwarder:

    • Dynamic destination selection (you specify the destination per request).
    • Customization of the HTTP client (HttpMessageInvoker).
    • Request and response customization (excluding bodies) via transforms.
    • Support for streaming protocols like gRPC and WebSockets.
    • Built-in error handling.

    What it does NOT include:

    • Routing
    • Load balancing
    • Affinity
    • Retries
  8. What is a DbContext and DbSet?

    main

    The DbContext is the main class that coordinates Entity Framework functionality for a given data model. It derives from Microsoft.EntityFrameworkCore.DbContext.

    Inside the DbContext, you define DbSet<TEntity> properties.

    • An entity set (represented by a DbSet) typically corresponds to a database table.
    • An entity corresponds to a single row in that table.

    Example: Changing a DbSet<Student> to DbSet<Students> updates the entity set name to be plural, which is a common convention.

  9. Overview of Razor Pages project structure

    main

    A standard Razor Pages project contains the following key components:

    • Pages folder: Contains Razor pages. Each page consists of a .cshtml file (HTML with Razor syntax) and a .cshtml.cs file (C# code-behind for handling page events). Supporting files like _Layout.cshtml (common UI elements) begin with an underscore.
    • wwwroot folder: Stores static assets such as HTML, JavaScript, and CSS files.
    • appsettings.json: Stores configuration data, such as database connection strings.
    • Program.cs: The entry point that configures the WebApplicationBuilder, adds Razor Pages to the Dependency Injection (DI) container, and defines the middleware pipeline.
  10. What is the Blazor WebAssembly Gateway

    main

    The Microsoft.AspNetCore.Components.Gateway package is a lightweight ASP.NET Core host designed for serving standalone Blazor WebAssembly apps in both development and production.

    Key features include:

    • Built-in SPA fallback routing: Automatically falls back to index.html for non-static asset requests, enabling client-side routing (e.g., /orders/42) to work on browser refresh.
    • Multiple Blazor WebAssembly clients: A single Gateway instance can host multiple clients under different path prefixes via the ClientApps section. This is used by .NET Aspire to host clients alongside backend services.
    • YARP integration: Includes YARP reverse-proxy infrastructure to forward backend traffic alongside the WebAssembly client.

    To use it in an existing standalone Blazor WebAssembly app (targeting .NET 11 or later), add a reference to the Microsoft.AspNetCore.Components.Gateway package. Fallback endpoints are enabled by default via the StaticWebAssetSpaFallbackEnabled property.

    <StaticWebAssetSpaFallbackEnabled>true</StaticWebAssetSpaFallbackEnabled>