教程:ASP.NET Core 入门Tutorial: Get started with ASP.NET Core

本教程展示了如何使用 .NET Core 命令行接口来创建并运行 ASP.NET Core Web 应用。This tutorial shows how to use the .NET Core command-line interface to create and run an ASP.NET Core web app.

你将了解如何: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 应用主页

系统必备Prerequisites

.NET Core 3.0 SDK 或更高版本.NET Core 3.0 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.cshtml ,并使用以下突出显示标记修改并保存页面: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: