本教程介绍具有控制器和视图的 ASP.NET Core MVC Web 开发。This tutorial teaches ASP.NET Core MVC web development with controllers and views.如果你是初次接触 ASP.NET Core Web 开发,请考虑本教程的 Razor Pages 版本,此版本提供了更简单的起点。If you're new to ASP.NET Core web development, consider the Razor Pages version of this tutorial, which provides an easier starting point.
这是本系列教程的第一个教程,介绍具有控制器和视图的 ASP.NET Core MVC Web 开发。This is the first tutorial of a series that teaches ASP.NET Core MVC web development with controllers and views.
在本系列结束时,你将拥有一个管理和显示电影数据的应用。At the end of the series, you'll have an app that manages and displays movie data.您将学习如何:You learn how to:
Visual Studio Code 说明使用用于 ASP.NET Core 的 .NET CLI 开发功能,如项目创建。The Visual Studio Code instructions use the .NET CLI for ASP.NET Core development functions such as project creation.可在(macOS、Linux 或 Windows)上或在任何代码编辑器中遵循这些说明。You can follow these instructions on macOS, Linux, or Windows and with any code editor.如果使用 Visual Studio Code 以外的其他内容,则可能需要进行少量更改。Minor changes may be required if you use something other than Visual Studio Code.
启动 Visual Studio 并选择“创建新项目”。Start Visual Studio and select Create a new project.
在“新建项目”对话框中,选择“ASP.NET Core Web 应用程序”>“下一步”。In the Create a new project dialog, select ASP.NET Core Web Application > Next.
在“配置新项目”对话框中,为“项目名称”输入 MvcMovie。In the Configure your new project dialog, enter MvcMovie for Project name.务必要将项目命名为“MvcMovie”。It's important to name the project MvcMovie.复制代码时,大小写需要匹配每个 namespace 匹配项。Capitalization needs to match each namespace matches when code is copied.
选择“创建”。Select Create.
在“创建新的 ASP.NET Core Web 应用程序”对话框中,选择:In the Create a new ASP.NET Core web application dialog, select:
下拉列表中的“.NET Core”和“ASP.NET Core 5.0”。.NET Core and ASP.NET Core 5.0 in the dropdowns.
ASP.NET Core Web 应用程序(模型-视图-控制器)。ASP.NET Core Web App (Model-View-Controller).
更改为将包含项目的目录 (cd)。Change to the directory (cd) that will contain the project.
运行以下命令:Run the following command:
dotnet new mvc -o MvcMovie
code -r MvcMovie
如果出现对话框,其中包含:“‘MvcMovie’中缺少进行生成和调试所需的资产。是否添加它们?” ,选择“是”If a dialog box appears with Required assets to build and debug are missing from 'MvcMovie'. Add them?, select Yes
dotnet new mvc -o MvcMovie:在 MvcMovie 文件夹中创建一个新的 ASP.NET Core MVC 项目。dotnet new mvc -o MvcMovie: Creates a new ASP.NET Core MVC project in the MvcMovie folder.
code -r MvcMovie:在 Visual Studio Code 中加载 MvcMovie.csproj 项目文件。code -r MvcMovie: Loads the MvcMovie.csproj project file in Visual Studio Code.
选择“文件”>“新建解决方案” 。Select File > New Solution.
在版本 8.6 之前的 Visual Studio for Mac 中,依次选择“.NET Core” > “应用” > “Web 应用程序(模型-视图-控制器)” > “下一步”。 In Visual Studio for Mac earlier than version 8.6, select .NET Core > App > Web Application (Model-View-Controller) > Next.在版本 8.6 或更高版本中,依次选择“Web 和控制台” > “应用” > “Web 应用程序(模型-视图-控制器)” > “下一步”。 In version 8.6 or later, select Web and Console > App > Web Application (Model-View-Controller) > Next.
在“配置新的 Web 应用”对话框中:In the Configure your new Web Application dialog:
确认已将“身份验证”设置为“无身份验证”。Confirm that Authentication is set to No Authentication.
如果看到用于选择“目标框架”的选项,请选择最新的 5.x 版本。If an option to select a Target Framework is presented, select the latest 5.x version.
选择“下一步”。Select Next.
将项目命名为“MvcMovie”,然后选择“创建”。 Name the project MvcMovie, and then select Create.
地址栏显示 localhost:port#,而不是显示 example.com。The address bar shows localhost:port# and not something like example.com.本地计算机的标准主机名为 localhost。The standard hostname for your local computer is localhost.当 Visual Studio 创建 Web 项目时,对 Web 服务器使用的是随机端口。When Visual Studio creates a web project, a random port is used for the web server.
在不进行调试的情况下,通过选择 Ctrl+F5 启动应用,可以:Launching the app without debugging by selecting Ctrl+F5 allows you to:
更改代码。Make code changes.
保存文件。Save the file.
快速刷新浏览器并查看代码更改。Quickly refresh the browser and see the code changes.
可以从“调试”菜单项中以调试或非调试模式启动应用:You can launch the app in debug or non-debug mode from the Debug menu item:
可以通过选择“IIS Express”按钮来调试应用You can debug the app by selecting the IIS Express button
下图显示该应用:The following image shows the app:
选择 Ctrl+F5,以在不使用调试程序的情况下运行。Select Ctrl+F5 to run without the debugger.
通过运行以下命令来信任 HTTPS 开发证书:Trust the HTTPS development certificate by running the following command:
dotnet dev-certs https --trust
上述命令在 Linux 上无效。The preceding command doesn't work on Linux.有关信任证书的详细信息,请参阅 Linux 发行版的文档。See your Linux distribution's documentation for trusting a certificate.
以上命令会显示以下对话:The preceding command displays the following dialog:
如果你同意信任开发证书,请选择“是”。Select Yes if you agree to trust the development certificate.
导航到 https://localhost:5001。Navigates to https://localhost:5001.
地址栏显示 localhost:port:5001,而不是显示 example.com。The address bar shows localhost:port:5001 and not something like example.com.本地计算机的标准主机名为 localhost。The standard hostname for your local computer is localhost.Localhost 仅为来自本地计算机的 Web 请求提供服务。Localhost only serves web requests from the local computer.
在不进行调试的情况下,通过选择 Ctrl+F5 启动应用,可以:Launching the app without debugging by selecting Ctrl+F5 allows you to:
更改代码。Make code changes.
保存文件。Save the file.
快速刷新浏览器并查看代码更改。Quickly refresh the browser and see the code changes.
选择“运行”>“开始执行(不调试)”以启动应用。Select Run > Start Without Debugging to launch the app.
地址栏显示 localhost:port#,而不是显示 example.com。The address bar shows localhost:port# and not something like example.com.本地计算机的标准主机名为 localhost。The standard hostname for your local computer is localhost.当 Visual Studio 创建 Web 项目时,对 Web 服务器使用的是随机端口。When Visual Studio creates a web project, a random port is used for the web server.
可以从“运行”菜单中以调试或非调试模式启动应用。You can launch the app in debug or non-debug mode from the Run menu.
本教程介绍具有控制器和视图的 ASP.NET Core MVC Web 开发。This tutorial teaches ASP.NET Core MVC web development with controllers and views.如果你是初次接触 ASP.NET Core Web 开发,请考虑本教程的 Razor Pages 版本,此版本提供了更简单的起点。If you're new to ASP.NET Core web development, consider the Razor Pages version of this tutorial, which provides an easier starting point.
这是本系列教程的第一个教程,介绍具有控制器和视图的 ASP.NET Core MVC Web 开发。This is the first tutorial of a series that teaches ASP.NET Core MVC web development with controllers and views.
在本系列结束时,你将拥有一个管理和显示电影数据的应用。At the end of the series, you'll have an app that manages and displays movie data.您将学习如何:You learn how to:
Visual Studio Code 说明使用用于 ASP.NET Core 的 .NET Core CLI 开发功能,如项目创建。The Visual Studio Code instructions use the .NET Core CLI for ASP.NET Core development functions such as project creation.可在任何平台(macOS、Linux 或 Windows)上或在任何代码编辑器中遵循这些说明。You can follow these instructions on any platform (macOS, Linux, or Windows) and with any code editor.如果使用 Visual Studio Code 以外的其他内容,则可能需要进行少量更改。Minor changes may be required if you use something other than Visual Studio Code.有关在 macOS 上安装 Visual Studio Code 的详细信息,请参阅 macOS 上的 Visual Studio Code。For more information on installing Visual Studio Code on macOS, see Visual Studio Code on macOS.
在 Visual Studio 中,选择“创建新项目”。From the Visual Studio, select Create a new project.
选择“ASP.NET Core Web 应用程序”>“下一步”。Select ASP.NET Core Web Application > Next.
将项目命名为“MvcMovie”,然后选择“创建” 。Name the project MvcMovie and select Create.将项目命名为“MvcMovie”非常重要,这样在复制代码时,命名空间才会匹配。It's important to name the project MvcMovie so when you copy code, the namespace will match.
选择“Web 应用(模型-视图-控制器)”。Select Web Application(Model-View-Controller).在下拉框中,选择“.NET Core”和“ASP.NET Core 3.1”,然后选择“创建”。From the dropdown boxes, select .NET Core and ASP.NET Core 3.1, then select Create.
Visual Studio 为创建的 MVC 项目使用默认项目模板。Visual Studio used the default project template for the created MVC project.创建的项目:The created project:
将目录 (cd) 更改为包含项目的文件夹。Change directories (cd) to a folder that will contain the project.
运行以下命令:Run the following command:
dotnet new mvc -o MvcMovie
code -r MvcMovie
一个对话框随即出现,其中包含:“‘MvcMovie’中缺少进行生成和调试所需的资产。是否添加它们?” ,选择“是”。A dialog box appears with Required assets to build and debug are missing from 'MvcMovie'. Add them?, select Yes.
dotnet new mvc -o MvcMovie:在 MvcMovie 文件夹中创建一个新的 ASP.NET Core MVC 项目。dotnet new mvc -o MvcMovie: Creates a new ASP.NET Core MVC project in the MvcMovie folder.
code -r MvcMovie:在 Visual Studio Code 中加载 MvcMovie.csproj 项目文件。code -r MvcMovie: Loads the MvcMovie.csproj project file in Visual Studio Code.
选择“文件”>“新建解决方案” 。Select File > New Solution.
在版本 8.6 之前的 Visual Studio for Mac 中,依次选择“.NET Core” > “应用” > “Web 应用程序(模型-视图-控制器)” > “下一步”。 In Visual Studio for Mac earlier than version 8.6, select .NET Core > App > Web Application (Model-View-Controller) > Next.在版本 8.6 或更高版本中,依次选择“Web 和控制台” > “应用” > “Web 应用程序(模型-视图-控制器)” > “下一步”。 In version 8.6 or later, select Web and Console > App > Web Application (Model-View-Controller) > Next.
在“配置新的 Web 应用”对话框中:In the Configure your new Web Application dialog:
确认已将“身份验证”设置为“无身份验证”。Confirm that Authentication is set to No Authentication.
如果看到用于选择“目标框架”的选项,请选择最新的 3.x 版本。If an option to select a Target Framework is presented, select the latest 3.x version.
选择“下一步”。Select Next.
将项目命名为“MvcMovie”,然后选择“创建”。 Name the project MvcMovie, and then select Create.
地址栏显示 localhost:port#,而不是显示 example.com。The address bar shows localhost:port# and not something like example.com.本地计算机的标准主机名为 localhost。The standard hostname for your local computer is localhost.当 Visual Studio 创建 Web 项目时,对 Web 服务器使用的是随机端口。When Visual Studio creates a web project, a random port is used for the web server.
在不进行调试的情况下,通过选择 Ctrl+F5 启动应用,可以:Launching the app without debugging by selecting Ctrl+F5 allows you to:
更改代码。Make code changes.
保存文件。Save the file.
快速刷新浏览器并查看代码更改。Quickly refresh the browser and see the code changes.
可以从“调试”菜单项中以调试或非调试模式启动应用:You can launch the app in debug or non-debug mode from the Debug menu item:
可以通过选择“IIS Express”按钮来调试应用You can debug the app by selecting the IIS Express button
下图显示该应用:The following image shows the app:
选择 Ctrl+F5 可运行应用,不会进行调试。Select Ctrl+F5 to run the app without debugging.
通过运行以下命令来信任 HTTPS 开发证书:Trust the HTTPS development certificate by running the following command:
dotnet dev-certs https --trust
上述命令在 Linux 上无效。The preceding command doesn't work on Linux.有关信任证书的详细信息,请参阅 Linux 发行版的文档。See your Linux distribution's documentation for trusting a certificate.
以上命令会显示以下对话:The preceding command displays the following dialog:
如果你同意信任开发证书,请选择“是”。Select Yes if you agree to trust the development certificate.
导航到 https://localhost:5001。Navigates to https://localhost:5001.
地址栏显示 localhost:port:5001,而不是显示 example.com。The address bar shows localhost:port:5001 and not something like example.com.本地计算机的标准主机名为 localhost。The standard hostname for your local computer is localhost.Localhost 仅为来自本地计算机的 Web 请求提供服务。Localhost only serves web requests from the local computer.
在不进行调试的情况下,通过选择 Ctrl+F5 启动应用,可以:Launching the app without debugging by selecting Ctrl+F5 allows you to:
更改代码。Make code changes.
保存文件。Save the file.
快速刷新浏览器并查看代码更改。Quickly refresh the browser and see the code changes.
选择“运行”>“开始执行(不调试)”以启动应用。Select Run > Start Without Debugging to launch the app.
Visual Studio for Mac:启动 Kestrel 服务器,启动浏览器并导航到 http://localhost:port,其中的 port 是随机选择的端口号。Visual Studio for Mac: starts Kestrel server, launches a browser, and navigates to http://localhost:port, where port is a randomly chosen port number.
Visual Studio for Mac 会显示以下弹出窗口:Visual Studio for Mac displays the following popup:
如果你信任开发证书,请选择“是”。Select Yes if you trust the development certificate.
将显示以下对话框:The following dialog is displayed:
输入你的密码,然后选择“确定”Enter your password and select OK
如果你同意信任开发证书,请选择“是”。Select Yes if you agree to trust the development certificate.
地址栏显示 localhost:port#,而不是显示 example.com。The address bar shows localhost:port# and not something like example.com.本地计算机的标准主机名为 localhost。The standard hostname for your local computer is localhost.Visual Studio 创建 Web 项目时,Web 服务器使用的是随机端口。When Visual Studio creates a web project, a random port is used for the web server.运行应用时,将看到不同的端口号。When you run the app, you'll see a different port number.
可以从“运行”菜单中以调试或非调试模式启动应用。You can launch the app in debug or non-debug mode from the Run menu.