教學課程:ASP.NET Core 使用者入門Tutorial: Get started with ASP.NET Core

本教學課程說明如何使用 .NET Core CLI 來建立和執行 ASP.NET Core web 應用程式。This tutorial shows how to create and run an ASP.NET Core web app using the .NET Core CLI.

您將學習如何:You'll learn how to:

  • 建立 Web 應用程式專案。Create a web app project.
  • 信任開發憑證。Trust the development certificate.
  • 執行應用程式。Run the app.
  • 編輯 Razor 頁面。Edit a Razor page.

最後,您會在本機電腦上執行一個運作正常的 Web 應用程式。At the end, you'll have a working web app running on your local machine.

Web 應用程式首頁

PrerequisitesPrerequisites

.NET Core 3.1 SDK 或更新版本.NET Core 3.1 SDK or later

建立 Web 應用程式專案Create a web app project

開啟命令殼層,並輸入下列命令:Open a command shell, and enter the following command:

dotnet new webapp -o aspnetcoreapp

上述命令︰The preceding command:

  • 建立新的 web 應用程式。Creates a new web app.
  • -o aspnetcoreapp參數會建立名為aspnetcoreapp的目錄,其中包含應用程式的原始程式檔。The -o aspnetcoreapp parameter creates a directory named aspnetcoreapp with the source files for the app.

信任開發憑證Trust the development certificate

信任 HTTPS 開發憑證:Trust the HTTPS development certificate:

dotnet dev-certs https --trust

上述命令會顯示以下對話方塊:The preceding command displays the following dialog:

安全性警告對話方塊

若您同意信任開發憑證,請選取 [是]****。Select Yes if you agree to trust the development certificate.

如需詳細資訊,請參閱信任 ASP.NET Core HTTPS 開發憑證For more information, see Trust the ASP.NET Core HTTPS development certificate

執行應用程式Run the app

執行下列命令:Run the following commands:

cd aspnetcoreapp
dotnet watch run

命令殼層指出應用程式已啟動之後,瀏覽到 https://localhost:5001After the command shell indicates that the app has started, browse to https://localhost:5001.

編輯 Razor 頁面Edit a Razor page

開啟 Pages/Index ,然後使用下列反白顯示的標記來修改並儲存頁面:Open Pages/Index.cshtml and modify and save the page with the following highlighted markup:

@page
@model IndexModel
@{
    ViewData["Title"] = "Home page";
}

<div class="text-center">
    <h1 class="display-4">Welcome</h1>
    <p>Hello, world! The time on the server is @DateTime.Now</p>
</div>

流覽至 https://localhost:5001 、重新整理頁面,然後確認是否顯示變更。Browse to https://localhost:5001, refresh the page, and verify the changes are displayed.

後續步驟Next steps

在本教學課程中,您已了解如何:In this tutorial, you learned how to:

  • 建立 Web 應用程式專案。Create a web app project.
  • 信任開發憑證。Trust the development certificate.
  • 執行專案。Run the project.
  • 進行變更。Make a change.

若要深入了解 ASP.NET Core,請參閱簡介中的建議學習路徑:To learn more about ASP.NET Core, see the recommended learning path in the introduction: