ASP.NET Core 簡介Introduction to ASP.NET Core

Daniel RothRick AndersonShaun Luttin 提供By Daniel Roth, Rick Anderson, and Shaun Luttin

ASP.NET Core 是一種跨平台且高效能的開放原始碼架構,用於建置現代化、雲端式、網際網路連線的應用程式。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 choose 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:

  • 用於建置 Web UI 和 Web API 的統一劇本。A unified story for building web UI and web APIs.
  • 可測試性架構。Architected for testability.
  • Razor Pages 更容易撰寫以頁面為焦點的案例程式碼,也更具生產力。Razor Pages makes coding page-focused scenarios easier and more productive.
  • 能夠在 Windows、macOS 和 Linux 上開發並執行。Ability to develop and run on Windows, macOS, and Linux.
  • 開放原始碼和社群導向 (英文)。Open-source and community-focused.
  • 整合現代化用戶端架構和開發工作流程。Integration of modern, client-side frameworks and development workflows.
  • 雲端就緒、以環境為基礎的組態系統A cloud-ready, environment-based configuration system.
  • 內建的相依性插入Built-in dependency injection.
  • 輕量型、高效能 (英文) 且模組化的 HTTP 要求管線。A lightweight, high-performance, and modular HTTP request pipeline.
  • 能夠在 IISNginxApacheDocker 上裝載,或自我裝載於您自己的處理序中。Ability to host on IIS, Nginx, Apache, Docker, or self-host in your own process.
  • .NET Core 為目標時,會有並存應用程式版本設定。Side-by-side app versioning when targeting .NET Core.
  • 可簡化現代網頁程式開發的工具。Tooling that simplifies modern web development.

使用 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 可完美整合常用的用戶端架構和程式庫,包括 BlazorAngularReactBootstrapASP.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.

實作 .NET Standard 2.0 的 .NET Framework 版本支援 ASP.NET Core 2.x:ASP.NET Core 2.x is supported on .NET Framework versions that implement .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 Core 較 .NET Framework 多的好處包含: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 相容性套件在 .NET Core 中發佈了上千個僅供 Windows 使用的 API。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 PagesGet started with Razor Pages
    Web 應用程式Web app 針對維護 MVC 應用程式For maintaining an MVC app 開始使用 MVCGet started with MVC
    Web APIWeb API 建立 Web API*Create 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 搭配 Entity Framework Core 的 MVCMVC with Entity Framework Core
  3. 閱讀適用於所有應用程式類型的 ASP.NET Core 功能概觀:Read an overview of ASP.NET Core features that apply to all app types:

  4. 瀏覽其他您感興趣主題的目錄。Browse the Table of Contents for other topics of interest.

*目前已有新的 Web API 教學課程,可讓您在瀏覽器中完整地遵循,而無須進行本機 IDE 安裝。* There is a new 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.

如何下載範例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-master.zip 檔案。Unzip the Docs-master.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 這兩個 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.

區域名稱通常包含字組 "snippet"。Region names usually contain the word "snippet." 下列範例顯示了名為 snippet_FilterInCode 的區域:The following example shows a region named snippet_FilterInCode:

#region snippet_FilterInCode
WebHost.CreateDefaultBuilder(args)
    .UseStartup<Startup>()
    .ConfigureLogging(logging =>
        logging.AddFilter("System", LogLevel.Debug)
            .AddFilter<DebugLoggerProvider>("Microsoft", LogLevel.Trace))
            .Build();
#endregion

上述的 C# 程式碼片段會在主題的 Markdown 檔案中,透過以下程式行加以參考: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_FilterInCode)]

您可以放心忽略 (或移除) 程式碼周圍的 #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.

如需詳細資訊,請參閱 Contribute to the ASP.NET documentation:Code snippets。(參與 ASP.NET 文件:程式碼片段)。For more information, see Contribute to the ASP.NET documentation: Code snippets.

後續步驟Next steps

如需詳細資訊,請參閱下列資源:For more information, see the following resources: