ASP.NET Core 简介Introduction to ASP.NET Core

作者:Daniel RothRick AndersonShaun LuttinBy Daniel Roth, Rick Anderson, and Shaun Luttin

ASP.NET Core 是一个跨平台的高性能开源框架,用于生成启用云且连接 Internet 的新式应用。ASP.NET Core is a cross-platform, high-performance, open-source framework for building modern, cloud-enabled, Internet-connected apps. 使用 ASP.NET Core,您可以:With ASP.NET Core, you can:

  • 生成 Web 应用和服务、物联网 (IoT) 应用和移动后端。Build web apps and services, Internet of Things (IoT) apps, and mobile backends.
  • 在 Windows、macOS 和 Linux 上使用喜爱的开发工具。Use your favorite development tools on Windows, macOS, and Linux.
  • 部署到云或本地。Deploy to the cloud or on-premises.
  • .NET Core 上运行。Run on .NET Core.

为何选择 ASP.NET Core?Why choose ASP.NET Core?

数百万开发人员在使用或使用过 ASP.NET 4.x 创建 Web 应用。Millions of developers use or have used ASP.NET 4.x to create web apps. ASP.NET Core 是对 ASP.NET 4.x 的重新设计,其中包括体系结构上的更改,产生了更精简、更模块化的框架。ASP.NET Core is a redesign of ASP.NET 4.x, including architectural changes that result in a leaner, more modular framework.

ASP.NET Core 具有如下优点:ASP.NET Core provides the following benefits:

使用 ASP.NET Core MVC 生成 Web API 和 Web UIBuild web APIs and web UI using ASP.NET Core MVC

ASP.NET Core MVC 提供生成 Web APIWeb 应用所需的功能:ASP.NET Core MVC provides features to build web APIs and web apps:

客户端开发Client-side development

ASP.NET Core 与常用客户端框架和库(包括 BlazorAngularReactBootstrap)无缝集成。ASP.NET Core integrates seamlessly with popular client-side frameworks and libraries, including Blazor, Angular, React, and Bootstrap. 有关详细信息,请参阅 ASP.NET Core Blazor 简介 和“客户端开发”下的相关主题。For more information, see ASP.NET Core Blazor 简介 and related topics under Client-side development.

ASP.NET Core 目标框架ASP.NET Core target frameworks

ASP.NET Core 3.x 和更高版本只能面向 .NET Core。ASP.NET Core 3.x and later can only target .NET Core. 通常,ASP.NET Core 由 .NET Standard 库组成。Generally, ASP.NET Core is composed of .NET Standard libraries. 使用 .NET Standard 2.0 编写的库在实现 .NET Standard 2.0 的任何 .NET 平台上运行。Libraries written with .NET Standard 2.0 run on any .NET platform that implements .NET Standard 2.0.

面向 .NET Core 有以下几个优势,并且这些优势会随着每次发布增加。There are several advantages to targeting .NET Core, and these advantages increase with each release. 与 .NET Framework 相比,.NET Core 的部分优势包括:Some advantages of .NET Core over .NET Framework include:

  • 跨平台。Cross-platform. 在 Windows、macOS 和 Linux 上运行。Runs on Windows, macOS, and Linux.
  • 性能更强Improved performance
  • 并行版本控制Side-by-side versioning
  • 新 APINew APIs
  • 开源Open source

建议通过以下一系列教程来了解如何开发 ASP.NET Core 应用:We recommend the following sequence of tutorials for an introduction to developing ASP.NET Core apps:

  1. 按照你要开发或维护的应用类型的教程操作:Follow a tutorial for the app type you want to develop or maintain.

    应用类型App type 方案Scenario 教程Tutorial
    Web 应用Web app 新的服务器端 Web UI 开发New server-side web UI development Razor Pages 入门Get started with Razor Pages
    Web 应用Web app 维护 MVC 应用Maintaining an MVC app MVC 入门Get started with MVC
    Web 应用Web app 客户端 Web UI 开发Client-side web UI development 开始使用 BlazorGet started with Blazor
    Web APIWeb API RESTful HTTP 服务RESTful HTTP services 创建 Web APICreate a web API
    远程过程调用应用Remote Procedure Call app 使用协议缓冲区的协定优先服务Contract-first services using Protocol Buffers 开始使用 gRPC 服务Get started with a gRPC service
    实时应用Real-time app 服务器和连接的客户端之间的双向通信Bidirectional communication between servers and connected clients 开始使用 SignalRGet started with SignalR
  2. 按照介绍如何进行基本数据访问的教程操作。Follow a tutorial that shows how to do basic data access.

    方案Scenario 教程Tutorial
    新的开发New development 带 Entity Framework Core 的 Razor 页面Razor Pages with Entity Framework Core
    维护 MVC 应用Maintaining an MVC app 结合使用 MVC 和 Entity Framework CoreMVC with Entity Framework Core
  3. 阅读适用于所有应用类型的 ASP.NET Core 基础知识的概述。Read an overview of ASP.NET Core fundamentals that apply to all app types.

  4. 浏览目录以了解其他感兴趣的主题。Browse the table of contents for other topics of interest.

†此外,还提供了一个交互式 Web API 教程†There's also an interactive web API tutorial. 无需在本地安装开发工具。No local installation of development tools is required. 代码在浏览器中的 Azure Cloud Shell 中运行,并且 curl 用于测试。The code runs in an Azure Cloud Shell in your browser, and curl is used for testing.

从 .NET Framework 迁移Migrate from .NET Framework

有关将 ASP.NET 4.x 应用迁移到 ASP.NET Core 的参考指南,请参阅 从 ASP.NET 迁移到 ASP.NET CoreFor a reference guide to migrating ASP.NET 4.x apps to ASP.NET Core, see 从 ASP.NET 迁移到 ASP.NET Core.

ASP.NET Core 是一个跨平台的高性能开源框架,用于生成启用云且连接 Internet 的新式应用。ASP.NET Core is a cross-platform, high-performance, open-source framework for building modern, cloud-enabled, Internet-connected apps. 使用 ASP.NET Core,您可以:With ASP.NET Core, you can:

为何选择 ASP.NET Core?Why choose ASP.NET Core?

数百万开发人员在使用或使用过 ASP.NET 4.x 创建 Web 应用。Millions of developers use or have used ASP.NET 4.x to create web apps. ASP.NET Core 是对 ASP.NET 4.x 的重新设计,通过体系结构上的更改,产生了更精简、更模块化的框架。ASP.NET Core is a redesign of ASP.NET 4.x, with architectural changes that result in a leaner, more modular framework.

ASP.NET Core 具有如下优点:ASP.NET Core provides the following benefits:

使用 ASP.NET Core MVC 生成 Web API 和 Web UIBuild web APIs and web UI using ASP.NET Core MVC

ASP.NET Core MVC 提供生成 Web APIWeb 应用所需的功能:ASP.NET Core MVC provides features to build web APIs and web apps:

客户端开发Client-side development

ASP.NET Core 与常用客户端框架和库(包括 BlazorAngularReactBootstrap)无缝集成。ASP.NET Core integrates seamlessly with popular client-side frameworks and libraries, including Blazor, Angular, React, and Bootstrap. 有关详细信息,请参阅 ASP.NET Core Blazor 简介 和“客户端开发”下的相关主题。For more information, see ASP.NET Core Blazor 简介 and related topics under Client-side development.

面向 .NET Framework 的 ASP.NET CoreASP.NET Core targeting .NET Framework

ASP.NET Core 2.x 可以面向 .NET Core 或 .NET Framework。ASP.NET Core 2.x can target .NET Core or .NET Framework. 面向 .NET Framework 的 ASP.NET Core 应用无法跨平台,它们仅在 Windows 上运行。ASP.NET Core apps targeting .NET Framework aren't cross-platform—they run on Windows only. 通常,ASP.NET Core 2.x 由 .NET Standard 库组成。Generally, ASP.NET Core 2.x is made up of .NET Standard libraries. 使用 .NET Standard 2.0 编写的库在实现 .NET Standard 2.0 的任何 .NET 平台上运行。Libraries written with .NET Standard 2.0 run on any .NET platform that implements .NET Standard 2.0.

ASP.NET Core 2.x 在实现 .NET Standard 2.0 的 .NET Framework 版本上受支持:ASP.NET Core 2.x is supported on .NET Framework versions that implement .NET Standard 2.0:

  • 建议使用最新版本的 .NET Framework。.NET Framework latest version is recommended.
  • .NET Framework 4.6.1 及更高版本。.NET Framework 4.6.1 and later.

ASP.NET Core 3.0 以及更高版本只能在 .NET Core 中运行。ASP.NET Core 3.0 and later will only run on .NET Core. 有关此更改的详细信息,请参阅 A first look at changes coming in ASP.NET Core 3.0(抢先了解 ASP.NET Core 3.0 即将推出的更改)。For more details regarding this change, see A first look at changes coming in ASP.NET Core 3.0.

面向 .NET Core 有以下几个优势,并且这些优势会随着每次发布增加。There are several advantages to targeting .NET Core, and these advantages increase with each release. 与 .NET Framework 相比,.NET Core 的部分优势包括:Some advantages of .NET Core over .NET Framework include:

  • 跨平台。Cross-platform. 在 macOS、Linux 和 Windows 上运行。Runs on macOS, Linux, and Windows.
  • 性能更强Improved performance
  • 并行版本控制Side-by-side versioning
  • 新 APINew APIs
  • 开源Open source

为了帮助缩小从 .NET Framework 到 .NET Core 的 API 差距,Windows 兼容包使数千个仅可在 Windows 中运行的 API 可在 .NET Core 中使用。To help close the API gap from .NET Framework to .NET Core, the Windows Compatibility Pack made thousands of Windows-only APIs available in .NET Core. 这些 API 在 .NET Core 1.x 中不可用。These APIs weren't available in .NET Core 1.x.

建议通过以下一系列教程和文章来了解如何开发 ASP.NET Core 应用:We recommend the following sequence of tutorials and articles for an introduction to developing ASP.NET Core apps:

  1. 按照你要开发或维护的应用类型的教程操作。Follow a tutorial for the type of app you want to develop or maintain.

    应用类型App type 方案Scenario 教程Tutorial
    Web 应用Web app 用于新的开发For new development Razor Pages 入门Get started with Razor Pages
    Web 应用Web app 用于维护 MVC 应用For maintaining an MVC app MVC 入门Get started with MVC
    Web APIWeb API 创建 Web APICreate a web API
    实时应用Real-time app 开始使用 SignalRGet started with SignalR
  2. 按照介绍如何进行基本数据访问的教程操作。Follow a tutorial that shows how to do basic data access.

    方案Scenario 教程Tutorial
    用于新的开发For new development 带 Entity Framework Core 的 Razor 页面Razor Pages with Entity Framework Core
    用于维护 MVC 应用For maintaining an MVC app 结合使用 MVC 和 Entity Framework CoreMVC with Entity Framework Core
  3. 阅读适用于所有应用类型的 ASP.NET Core 基础知识的概述。Read an overview of ASP.NET Core fundamentals that apply to all app types.

  4. 浏览目录以了解其他感兴趣的主题。Browse the Table of Contents for other topics of interest.

†此外,还提供了一个 Web API 教程,需要在浏览器中完全遵循,无需在本地安装 IDE。†There's also a web API tutorial that you follow entirely in the browser, no local IDE installation required. 代码在 Azure Cloud Shell 中运行,并且 curl 用于测试。The code runs in an Azure Cloud Shell, and curl is used for testing.

从 .NET Framework 迁移Migrate from .NET Framework

有关将 ASP.NET 应用迁移到 ASP.NET Core 的参考指南,请参阅 从 ASP.NET 迁移到 ASP.NET CoreFor a reference guide to migrating ASP.NET apps to ASP.NET Core, see 从 ASP.NET 迁移到 ASP.NET Core.

如何下载示例How to download a sample

很多文章和教程中都包含有示例代码链接。Many of the articles and tutorials include links to sample code.

  1. 下载 ASP.NET 存储库 zip 文件Download the ASP.NET repository zip file.
  2. 解压 Docs-main.zip 文件。Unzip the Docs-main.zip file.
  3. 使用示例链接中的 URL 帮助你导航到示例目录。Use the URL in the sample link to help you navigate to the sample directory.

示例代码中的预处理器指令Preprocessor directives in sample code

为了演示多个方案,示例应用使用 #define#if-#else/#elif-#endif 预处理器指令选择性地编译和运行示例代码中不同的片段。To demonstrate multiple scenarios, sample apps use the #define and #if-#else/#elif-#endif preprocessor directives to selectively compile and run different sections of sample code. 对于那些利用此方法的示例,请将 C# 文件顶部的 #define 指令设置为定义与你想要运行的方案相关联的符号。For those samples that make use of this approach, set the #define directive at the top of the C# files to define the symbol associated with the scenario that you want to run. 一些示例要求在多个文件的顶部定义符号才能运行方案。Some samples require defining the symbol at the top of multiple files in order to run a scenario.

例如,以下 #define 符号列表指示四个方案可用(每个符号一个方案)。For example, the following #define symbol list indicates that four scenarios are available (one scenario per symbol). 当前示例配置运行 TemplateCode 方案:The current sample configuration runs the TemplateCode scenario:

#define TemplateCode // or LogFromMain or ExpandDefault or FilterInCode

若要更改示例以运行 ExpandDefault 方案,请定义 ExpandDefault 符号并保留剩余的符号处于被注释掉的状态:To change the sample to run the ExpandDefault scenario, define the ExpandDefault symbol and leave the remaining symbols commented-out:

#define ExpandDefault // TemplateCode or LogFromMain or FilterInCode

若要详细了解如何使用 C# 预处理器指令选择性地编译代码段,请参阅 #define(C# 参考)#if(C# 参考)For more information on using C# preprocessor directives to selectively compile sections of code, see #define (C# Reference) and #if (C# Reference).

示例代码中的区域Regions in sample code

一些示例应用包含由 #region#endregion C# 指令包围的代码片段。Some sample apps contain sections of code surrounded by #region and #endregion C# directives. 文档生成系统会将这些区域注入到所呈现的文档主题中。The documentation build system injects these regions into the rendered documentation topics.

区域名称通常包含“代码段”一词。Region names usually contain the word "snippet." 下面的示例显示了一个名为 snippet_WebHostDefaults 的区域:The following example shows a region named snippet_WebHostDefaults:

#region snippet_WebHostDefaults
Host.CreateDefaultBuilder(args)
    .ConfigureWebHostDefaults(webBuilder =>
    {
        webBuilder.UseStartup<Startup>();
    });
#endregion

主题的 Markdown 文件在以下行中引用了前面的 C# 代码片段:The preceding C# code snippet is referenced in the topic's markdown file with the following line:

[!code-csharp[](sample/SampleApp/Program.cs?name=snippet_WebHostDefaults)]

可放心忽略(或删除)代码两侧的 #region#endregion 指令。You may safely ignore (or remove) the #region and #endregion directives that surround the code. 如果计划运行主题中所述的示例方案,请不要更改这些指令中的代码。Don't alter the code within these directives if you plan to run the sample scenarios described in the topic. 试用其他方案时,可随时更改代码。Feel free to alter the code when experimenting with other scenarios.

有关详细信息,请参阅参与 ASP.NET 文档:代码片段For more information, see Contribute to the ASP.NET documentation: Code snippets.

重大更改和安全建议Breaking changes and security advisories

公告存储库上报告了重大更改和安全建议。Breaking changes and security advisories are reported on the Announcements repo. 可以通过选择“标签”筛选器将公告限制为特定版本。Announcements can be limited to a specific version by selecting a Label filter.

后续步骤Next steps

有关更多信息,请参见以下资源:For more information, see the following resources: