# ASP.NET Core

ASP.NET Core 是一个开源、跨平台的框架，专为创建当代基于云、连接互联网的软件而设计，包括 Web 应用、IoT 应用和移动后端；它运行在 .NET 上，后者是一个同样跨平台的免费运行时。<sup>[1](https://github.com/dotnet/aspnetcore)</sup> 微软将其描述为一个功能完备、高性能的 Web 框架，作为 .NET 平台的一部分发布。<sup>[2](https://learn.microsoft.com/en-us/shows/back-end-web-development-with-dotnet-for-beginners/what-is-aspnet-core-back-end)</sup> 它是较旧的 ASP.NET Framework（常被称为 ASP.NET 4.x）的现代替代品，后者仅在 Windows 和 .NET Framework 上运行。<sup>[3](https://learn.microsoft.com/aspnet/overview)</sup>

| 关键事实 | 详情 |
|---|---|
| 平台 | 开源且跨平台；应用在 Windows、Mac 和 Linux 上运行于 .NET<sup>[1](https://github.com/dotnet/aspnetcore)</sup> |
| 默认服务器 | Kestrel，一个跨平台 HTTP 服务器，包含在项目模板中并默认启用<sup>[4](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel?preserve-view=true&view=aspnetcore-10.0)</sup> |
| 请求管道 | 一系列中间件请求委托，每个委托可在下一个之前和之后执行操作，并可短路管道<sup>[5](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/middleware/?view=aspnetcore-12.0)</sup> |
| 内置服务 | 依赖注入、一个读取 JSON 文件、环境变量和命令行参数的配置框架，以及日志记录<sup>[6](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/?view=aspnetcore-10.0)</sup> |
| API 风格 | 最小 API（推荐用于新项目）和基于控制器的 API（派生自 ControllerBase）<sup>[7](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/minimal-apis/overview?view=aspnetcore-10.0)</sup> |
| 吞吐量 | 在 TechEmpower 基准测试中，最小 API 每秒处理 205 万条 JSON 响应，而 Node.js 为 22.4 万，Java Servlet 为 32.8 万<sup>[8](https://dotnet.microsoft.com/en-us/learn/aspnet/what-is-aspnet-core)</sup> |
| 支持周期 | ASP.NET Core 版本跟随 .NET 发布；.NET 10 为长期支持版，支持至 2028 年 11 月<sup>[9](https://learn.microsoft.com/en-us/dotnet/core/releases-and-support)</sup> |

## 架构与基础

ASP.NET Core 应用围绕微软基础概述中描述的三个框架服务构建：依赖注入（DI）、配置和中间件。<sup>[6](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/?view=aspnetcore-10.0)</sup> 启动代码位于 `Program` 文件（`Program.cs`）中，应用在此配置其服务并定义请求处理管道。<sup>[6](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/?view=aspnetcore-10.0)</sup> 典型的入口点通过 `WebApplication.CreateBuilder(args)` 初始化一个带有预配置默认值的 `WebApplicationBuilder`。<sup>[6](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/?view=aspnetcore-10.0)</sup>

**中间件。** 请求管道由一系列依次调用的请求委托组成。每个中间件决定是否将请求传递给下一个组件，并可在其前后执行工作；短路管道的中间件称为终端中间件。委托通过 `Run`、`Map` 和 `Use` 扩展方法配置，可内联或作为可复用类。中间件的添加顺序决定了请求的处理顺序和响应的反向顺序，该顺序对安全性、性能和功能至关重要；异常处理委托应尽早注册，以便捕获后续阶段的异常。<sup>[5](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/middleware/?view=aspnetcore-12.0)</sup>

**依赖注入。** 框架包含内置的服务容器 `IServiceProvider`。服务在 `Program` 文件（.NET 6 或更高版本）或 `Startup` 文件（.NET 5 或更早版本）中注册，框架负责创建、注入和释放依赖实例。对于基于 ASP.NET Core 模板的应用，框架在应用添加自身服务之前已注册超过 250 个服务。<sup>[10](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-10.0&wt.mc_id=MVP_407589)</sup>

**配置。** 配置框架从有序的提供程序集合中以名称-值对形式获取设置，内置支持 JSON 文件、XML 文件、环境变量和命令行参数；可添加自定义提供程序。默认情况下，应用从 `appsettings.json`、环境变量和命令行读取，环境变量会覆盖设置文件中的值。<sup>[6](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/?view=aspnetcore-10.0)</sup>

## 服务器与托管

ASP.NET Core 应用使用 HTTP 服务器实现来监听请求；服务器将请求作为请求特性组合到 `HttpContext` 中呈现给应用。<sup>[6](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/?view=aspnetcore-10.0)</sup> 在 Windows 上，有三种服务器实现可用：Kestrel、IIS HTTP Server（应用和 IIS 在同一进程中运行）和 HTTP.sys（不与 IIS 一起使用）。在 macOS 和 Linux 上，Kestrel 是提供的实现。<sup>[6](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/?view=aspnetcore-10.0)</sup>

**Kestrel** 是默认的跨平台 HTTP 服务器，包含在项目模板中并默认启用。自 ASP.NET Core 2.0 起，它可作为直接面向互联网的公共边缘服务器运行，也常运行在 IIS、Nginx 或 Apache 等反向代理之后。<sup>[4](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel?preserve-view=true&view=aspnetcore-10.0)</sup> 无论是否使用反向代理，两种配置均受支持。<sup>[11](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel/when-to-use-a-reverse-proxy?view=aspnetcore-3.1)</sup>

反向代理从网络接收 HTTP 请求并将其转发给 Kestrel。即使并非必需，使用反向代理也可以限制托管应用的公共暴露面、增加一层配置和纵深防御安全、与现有基础设施集成，并简化负载均衡和 HTTPS 配置，因为只有代理需要公共 X.509 证书，且它可以通过纯 HTTP 或使用本地管理证书的 HTTPS 与应用服务器通信。作为无代理的边缘服务器，Kestrel 不支持在多个进程间共享同一 IP 地址和端口；它处理配置端口上的所有流量，而不论 `Host` 头如何。在反向代理后托管需要配置主机过滤（转发头）。<sup>[11](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel/when-to-use-a-reverse-proxy?view=aspnetcore-3.1)</sup>

**IIS 托管**支持两种模型。进程内托管使用 IIS HTTP Server 在 IIS 工作进程内运行应用，由于请求不通过回环适配器代理，性能得到提升；ASP.NET Core 模块加载 CoreCLR，调用 `Program.Main`，并处理 IIS 原生请求生命周期。在进程外模型中，模块在动态 localhost 端口上启动应用的 Kestrel 进程并转发请求；模块不支持 HTTPS 转发，因此即使 IIS 通过 HTTPS 接收请求，请求也通过 HTTP 转发。应用内容根目录下的 `web.config` 文件配置该模块，且在生产环境中必须保持存在且命名正确，否则 IIS 可能会公开提供 `*.runtimeconfig.json` 和 `*.deps.json` 等敏感文件。<sup>[12](https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/iis/?view=aspnetcore-10.0)</sup>

## Web UI 与 API 框架

ASP.NET Core 提供多种构建应用用户界面和 API 的方式。<sup>[13](https://learn.microsoft.com/en-us/aspnet/core/tutorials/choose-web-ui?view=aspnetcore-10.0)</sup>

- **Blazor** 是一个基于可复用组件模型的全栈 Web UI 框架，推荐用于大多数 Web UI 场景。组件可在服务器端渲染，或通过 [WebAssembly](https://www.edgepedia.org/zh/webassembly) 在客户端渲染，Blazor Server 通过 SignalR 连接处理 UI 更新，而 .NET 运行时留在服务器上。<sup>[13](https://learn.microsoft.com/en-us/aspnet/core/tutorials/choose-web-ui?view=aspnetcore-10.0)</sup><sup> • </sup><sup>[14](https://learn.microsoft.com/aspnet/core/blazor/?view=aspnetcore-10.0)</sup>
- **Razor Pages** 是一种基于页面的服务器渲染 UI 模型，构建于 ASP.NET Core MVC 之上；`@page` 指令使文件无需控制器即可直接处理请求。<sup>[13](https://learn.microsoft.com/en-us/aspnet/core/tutorials/choose-web-ui?view=aspnetcore-10.0)</sup><sup> • </sup><sup>[15](https://learn.microsoft.com/en-us/aspnet/core/razor-pages/index)</sup>
- **MVC** 是一个丰富的框架，用于以模型-视图-控制器模式构建 Web 应用和 API，使用 Razor 视图引擎在 HTML 标记中嵌入 .NET 代码。<sup>[16](https://learn.microsoft.com/en-us/aspnet/core/mvc/overview?view=aspnetcore-10.0)</sup>
- **最小 API** 让开发者以最少的代码流畅地声明 REST 端点，例如 `app.MapGet("/", () => "Hello World!");`，是新 API 项目的推荐方法。**基于控制器的 API** 使用派生自 `ControllerBase` 的类，在需要模型绑定扩展性、高级验证、应用程序部件或 [OData](https://www.edgepedia.org/zh/odata) 支持时，仍是开箱即用的选择。<sup>[7](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/minimal-apis/overview?view=aspnetcore-10.0)</sup>

## 缓存

ASP.NET Core 提供多种缓存机制。<sup>[17](https://learn.microsoft.com/en-us/aspnet/core/performance/caching/overview?view=aspnetcore-8.0)</sup> 响应缓存中间件遵循由 `Cache-Control` 头驱动的标准 HTTP 缓存语义，适用于公共 GET 或 HEAD API 请求，但对 UI 应用通常无效，因为浏览器常发送 `Cache-control: max-age=0` 等阻止缓存的头。<sup>[17](https://learn.microsoft.com/en-us/aspnet/core/performance/caching/overview?view=aspnetcore-8.0)</sup>

**输出缓存**在 .NET 7 及更高版本中可用，在客户端无法覆盖的服务器端配置下缓存 HTTP 响应。其存储介质可扩展（默认为内存），选定条目可通过编程方式失效，资源锁定可缓解缓存击穿和惊群效应，缓存重新验证可返回 304 Not Modified 状态以节省带宽。<sup>[17](https://learn.microsoft.com/en-us/aspnet/core/performance/caching/overview?view=aspnetcore-8.0)</sup> 默认情况下，输出缓存仅存储对 GET 或 HEAD 请求的 HTTP 200 响应，不存储设置 Cookie 或来自已认证请求的响应。<sup>[18](https://learn.microsoft.com/en-us/aspnet/core/performance/caching/output?preserve-view=true&view=aspnetcore-10.0)</sup> 分布式缓存可用于 Web 场，支持 SQL Server、Redis、Postgres 和 NCache。<sup>[17](https://learn.microsoft.com/en-us/aspnet/core/performance/caching/overview?view=aspnetcore-8.0)</sup>

## 安全

身份验证确定用户身份，授权确定该用户可执行的操作；两者相互独立，但授权依赖身份验证。身份验证由 `IAuthenticationService` 通过称为方案的已注册处理程序处理，如 Cookie 和 JWT 持有者方案，以及包括 Facebook、Google、微软和其他 OIDC 提供程序在内的外部提供程序。<sup>[19](https://learn.microsoft.com/en-us/aspnet/core/security/authentication/?view=aspnetcore-10.0)</sup> [授权](https://www.edgepedia.org/zh/authorization)通过 `[Authorize]` 和 `[AllowAnonymous]` 特性以及基于策略的模型以声明方式表达，在策略模型中处理程序根据要求评估用户的声明。<sup>[20](https://learn.microsoft.com/en-us/aspnet/core/security/authorization/?view=aspnetcore-7.0)</sup>

**ASP.NET Core Identity** 是一个支持 UI 登录功能并管理用户、密码、个人资料数据、角色、声明、令牌和电子邮件确认的 API；通常配置 SQL Server 数据库，并支持 Facebook、Google、Microsoft Account 和 Twitter 的外部登录。对于保护 Web API 和单页应用，微软推荐 Microsoft Entra ID 或 Duende Identity Server，后者是面向 ASP.NET Core 的 [OpenID Connect](https://www.edgepedia.org/zh/openid-connect) 和 [OAuth 2.0](https://www.edgepedia.org/zh/oauth-2-0) 框架，生产使用可能需要许可费。<sup>[21](https://learn.microsoft.com/aspnet/core/security/authentication/identity)</sup> OWASP DotNet 安全备忘单指出，Identity 默认配置良好，使用 PBKDF2 密码哈希并为每个用户使用随机盐。<sup>[22](https://cheatsheetseries.owasp.org/cheatsheets/DotNet_Security_Cheat_Sheet.html)</sup>

**数据保护**是一个用于保护数据的加密 API，包括密钥管理和轮换，设计上使开发者无需学习密钥管理原则；它取代了 ASP.NET 1.x 至 4.x 中的 `<machineKey>` 元素。其典型用例是通过不受信任的客户端往返传递受信任状态，如身份验证 Cookie 或持有者令牌。<sup>[23](https://learn.microsoft.com/en-us/aspnet/core/security/data-protection/introduction?view=aspnetcore-5.0)</sup> 在开发环境中，Secret Manager 工具将应用机密存储在用户配置文件中的 JSON 文件里，位于源代码管理之外且不加密，而生产机密应通过 Azure Key Vault 等受控方式访问。<sup>[24](https://learn.microsoft.com/en-us/aspnet/core/security/app-secrets?view=aspnetcore-10.0)</sup>

## 性能

微软的 .NET 网站引用了独立的 TechEmpower 基准测试，其中 ASP.NET Core 最小 API 每秒处理 205 万条 JSON 响应，而 Node.js 为 22.4 万，Java Servlet 为 32.8 万。<sup>[8](https://dotnet.microsoft.com/en-us/learn/aspnet/what-is-aspnet-core)</sup> 微软的性能指南指出，本可异步的阻塞调用是常见的性能问题，并推荐使用 Visual Studio 诊断工具或 PerfView 等分析工具来识别热代码路径。<sup>[25](https://learn.microsoft.com/en-us/aspnet/core/performance/performance-best-practices?view=aspnetcore-6.0)</sup>

## 与 ASP.NET Framework 的关系及迁移

ASP.NET Core 是经典 ASP.NET 的替代方案，后者在 .NET Framework 上提供 Web Forms、MVC 和 Web Pages 框架。<sup>[3](https://learn.microsoft.com/aspnet/overview)</sup> 微软表示，对大多数生产应用而言，将生产应用从 ASP.NET Framework 迁移到 ASP.NET Core 并非易事：`System.Web` 依赖遍布代码库，会话和身份验证 API 根本不同，HTTP 模块和处理程序必须变为中间件，库依赖链必须按后序深度优先顺序升级。文档记录了两种迁移路径：增量迁移，即绞杀榕模式的实现，适用于必须在生产环境中持续运行的大型应用；以及就地迁移，对于足够小的应用可快速替换。<sup>[26](https://learn.microsoft.com/en-us/aspnet/core/migration/proper-to-2x/?view=aspnetcore-3.1)</sup> 某些行为也有微妙差异，例如 `%5C` 和 `%2F` 的 URI 解码，以及 ASP.NET Core 不会自动为每个请求设置 `CultureInfo.CurrentCulture`，因此必须显式添加 `app.UseRequestLocalization()` 等本地化中间件。<sup>[27](https://learn.microsoft.com/en-us/aspnet/core/migration/fx-to-core/areas/misc?view=aspnetcore-10.0)</sup>

2020 年 .NET 5 统一 .NET Core 和 .NET Framework 路径时，运行时去掉了“Core”名称，但 ASP.NET Core 5.0 保留了它，以避免与 [ASP.NET MVC](https://www.edgepedia.org/zh/asp-net-mvc) 5 混淆。<sup>[28](https://learn.microsoft.com/en-us/dotnet/core/dotnet-five)</sup> 此后 ASP.NET Core 版本跟随 .NET 发布：.NET 8（2023 年 11 月发布）和 .NET 10 为长期支持版本，.NET 10 支持至 2028 年 11 月。<sup>[9](https://learn.microsoft.com/en-us/dotnet/core/releases-and-support)</sup>

## References

1. [dotnet/aspnetcore repository README, GitHub](https://github.com/dotnet/aspnetcore)
2. [What is ASP.NET Core?, Microsoft Learn](https://learn.microsoft.com/en-us/shows/back-end-web-development-with-dotnet-for-beginners/what-is-aspnet-core-back-end)
3. [ASP.NET overview, Microsoft Learn](https://learn.microsoft.com/aspnet/overview)
4. [Kestrel web server in ASP.NET Core, Microsoft Learn](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel?preserve-view=true&view=aspnetcore-10.0)
5. [ASP.NET Core middleware, Microsoft Learn](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/middleware/?view=aspnetcore-12.0)
6. [ASP.NET Core fundamentals overview, Microsoft Learn](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/?view=aspnetcore-10.0)
7. [APIs overview, Microsoft Learn](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/minimal-apis/overview?view=aspnetcore-10.0)
8. [What is ASP.NET Core?, .NET](https://dotnet.microsoft.com/en-us/learn/aspnet/what-is-aspnet-core)
9. [.NET releases, patches, and support, Microsoft Learn](https://learn.microsoft.com/en-us/dotnet/core/releases-and-support)
10. [Dependency injection in ASP.NET Core, Microsoft Learn](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-10.0&wt.mc_id=MVP_407589)
11. [When to use a reverse proxy with Kestrel, Microsoft Learn](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel/when-to-use-a-reverse-proxy?view=aspnetcore-3.1)
12. [Host ASP.NET Core on Windows with IIS, Microsoft Learn](https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/iis/?view=aspnetcore-10.0)
13. [Choose an ASP.NET Core web UI, Microsoft Learn](https://learn.microsoft.com/en-us/aspnet/core/tutorials/choose-web-ui?view=aspnetcore-10.0)
14. [ASP.NET Core Blazor, Microsoft Learn](https://learn.microsoft.com/aspnet/core/blazor/?view=aspnetcore-10.0)
15. [Razor Pages architecture and concepts, Microsoft Learn](https://learn.microsoft.com/en-us/aspnet/core/razor-pages/index)
16. [Overview of ASP.NET Core MVC, Microsoft Learn](https://learn.microsoft.com/en-us/aspnet/core/mvc/overview?view=aspnetcore-10.0)
17. [Overview of caching in ASP.NET Core, Microsoft Learn](https://learn.microsoft.com/en-us/aspnet/core/performance/caching/overview?view=aspnetcore-8.0)
18. [Output caching middleware in ASP.NET Core, Microsoft Learn](https://learn.microsoft.com/en-us/aspnet/core/performance/caching/output?preserve-view=true&view=aspnetcore-10.0)
19. [Overview of ASP.NET Core Authentication, Microsoft Learn](https://learn.microsoft.com/en-us/aspnet/core/security/authentication/?view=aspnetcore-10.0)
20. [Introduction to authorization in ASP.NET Core, Microsoft Learn](https://learn.microsoft.com/en-us/aspnet/core/security/authorization/?view=aspnetcore-7.0)
21. [Introduction to Identity on ASP.NET Core, Microsoft Learn](https://learn.microsoft.com/aspnet/core/security/authentication/identity)
22. [DotNet Security Cheat Sheet, OWASP](https://cheatsheetseries.owasp.org/cheatsheets/DotNet_Security_Cheat_Sheet.html)
23. [ASP.NET Core Data Protection Overview, Microsoft Learn](https://learn.microsoft.com/en-us/aspnet/core/security/data-protection/introduction?view=aspnetcore-5.0)
24. [Safe storage of app secrets in development, Microsoft Learn](https://learn.microsoft.com/en-us/aspnet/core/security/app-secrets?view=aspnetcore-10.0)
25. [ASP.NET Core Best Practices, Microsoft Learn](https://learn.microsoft.com/en-us/aspnet/core/performance/performance-best-practices?view=aspnetcore-6.0)
26. [Migrate from ASP.NET Framework to ASP.NET Core, Microsoft Learn](https://learn.microsoft.com/en-us/aspnet/core/migration/proper-to-2x/?view=aspnetcore-3.1)
27. [Technical differences between ASP.NET Framework and ASP.NET Core, Microsoft Learn](https://learn.microsoft.com/en-us/aspnet/core/migration/fx-to-core/areas/misc?view=aspnetcore-10.0)
28. [What's new in .NET 5, Microsoft Learn](https://learn.microsoft.com/en-us/dotnet/core/dotnet-five)

---
*Initially written Sep 17, 2026 · Reviewed: Oct 15, 2026 · Edited: Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Development tools and collaboration infrastructure › Frameworks, libraries, and APIs › Web frameworks and libraries

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/asp-net-core

English original: https://www.edgepedia.org/asp-net-core
