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-based, Internet-connected applications. 使用 ASP.NET Core,您可以:With ASP.NET Core, you can:

  • 创建 Web 应用程序和服务、IoT 应用和移动后端。Build web apps and services, 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 或 .NET Framework 上运行。Run on .NET Core or .NET Framework.

为何使用 ASP.NET Core?Why use ASP.NET Core?

数百万开发人员使用过(并将继续使用)ASP.NET 4.x 创建 Web 应用。Millions of developers have used (and continue to use) 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 与常用客户端框架和库(包括 Razor ComponentsAngularReactBootstrap)无缝集成。ASP.NET Core integrates seamlessly with popular client-side frameworks and libraries, including Razor Components, Angular, React, and Bootstrap. 有关详细信息,请参阅 Razor Components 和“客户端开发”下的相关主题。For more information, see Razor Components 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 支持的任何位置运行。Apps written with .NET Standard 2.0 run anywhere that .NET Standard 2.0 is supported.

与 .NET Standard 2.0 兼容的 .NET Framework 版本支持 ASP.NET Core 2.x:ASP.NET Core 2.x is supported on .NET Framework versions compatible with .NET Standard 2.0:

  • 强烈建议使用 .NET Framework 4.7.1 及更高版本。.NET Framework 4.7.1 and later is strongly 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 差距。We're working hard to close the API gap from .NET Framework to .NET Core. Windows 兼容性包使数千个仅可在Windows运行的API 可在 .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.

如何下载示例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. 解压缩 文件。Unzip the 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 C# 语句选择性地编译和运行不同的示例代码段。To demonstrate multiple scenarios, sample apps use the #define and #if-#else/#elif-#endif C# statements to selectively compile and run different sections of sample code. 对于那些利用此方法的示例,请将 C# 文件顶部的 #define 语句设置为与你想要运行的方案相关联的符号。For those samples that make use of this approach, set the #define statement at the top of the C# files to the symbol associated with the scenario that you want to run. 一些示例要求在多个文件的顶部设置符号才能运行方案。Some samples require setting 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# statements. 文档生成系统会将这些区域注入到所呈现的文档主题中。The documentation build system injects these regions into the rendered documentation topics.

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

#region snippet_FilterInCode
    .ConfigureLogging(logging =>
        logging.AddFilter("System", LogLevel.Debug)
            .AddFilter<DebugLoggerProvider>("Microsoft", LogLevel.Trace))

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


你可放心忽略(或删除)代码两侧的 #region#endregion 语句。You may safely ignore (or remove) the #region and #endregion statements that surround the code. 如果计划运行主题中所述的示例方案,请不要更改这些语句中的代码。Don't alter the code within these statements 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.

后续步骤Next steps

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