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-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 與熱門的用戶端架構和程式庫緊密整合,包括 Blazor角度回應啟動程式。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 目標 frameworkASP.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 Core 較 .NET Framework 多的好處包含: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 頁面Get started with Razor Pages
    Web 應用程式Web app 維護 MVC 應用程式Maintaining an MVC app 開始使用 MVCGet 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 Razor 具有 Entity Framework Core 的頁面Razor Pages with Entity Framework Core
    維護 MVC 應用程式Maintaining an MVC app 搭配 Entity Framework Core 的 MVCMVC 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 ,並使用 捲曲 來進行測試。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 是一種跨平臺、高效能的 開放原始 碼架構,可用於建立現代化、具備雲端功能的網際網路連線應用程式。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 與熱門的用戶端架構和程式庫緊密整合,包括 Blazor角度回應啟動程式。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.

實作 .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 最新版本。.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 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

為了協助您關閉 API 間距,從 .NET Framework 到 .NET Core, 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 頁面Get started with Razor Pages
    Web 應用程式Web app 針對維護 MVC 應用程式For maintaining an MVC app 開始使用 MVCGet 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 Razor 具有 Entity Framework Core 的頁面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 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-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 選擇性地編譯及執行範例程式碼的不同區段。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.

區域名稱通常包含字組 "snippet"。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

上述的 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_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.

如需詳細資訊,請參閱 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: