在 ASP.NET Core 中开始使用 Razor Pages

 

https://docs.microsoft.com/zh-cn/aspnet/core/tutorials/razor-pages/razor-pages-start?view=aspnetcore-2.1

 

Select the ASP.NET and web development workload.::: moniker range=">= aspnetcore-2.1" .Net Core 2.1 SDK ::: moniker-end]

创建 Razor Web 应用

  • 从 Visual Studio“文件”菜单中,选择“新建”>“项目”。
  • 创建新的 ASP.NET Core Web 应用程序。 将项目命名为“RazorPagesMovie”。 将项目命名为“RazorPagesMovie”,以便命名空间在你复制/粘贴代码时相互匹配。 新建 ASP.NET Core Web 应用程序
  • 在下拉列表中选择“ASP.NET Core 2.1”,然后选择“Web 应用程序”。

新建 ASP.NET Core Web 应用程序

Visual Studio 模板创建初学者项目:

“解决方案资源管理器”

按 F5 在调试模式下运行应用,或按 Ctrl-F5 在不附加调试器的情况下运行。 选择“接受”以同意跟踪。 此应用不会跟踪个人信息。 模板生成的代码包含有助于符合一般数据保护条例 (GDPR) 的资产。

主页或索引页

下图展示了接受跟踪后的应用:

主页或索引页

  • Visual Studio 启动 IIS Express 并运行应用。 地址栏显示 localhost:port#,而不是显示 example.com。 这是因为 localhost 是本地计算机的标准主机名。 Localhost 仅为来自本地计算机的 Web 请求提供服务。 Visual Studio 创建 Web 项目时,为 Web 服务器使用随机端口。 在上图中,端口号为 5000。 运行应用时,将看到不同的端口号。
  • 使用“Ctrl+F5”启动应用(非调试模式)后,可执行代码更改、保存文件、刷新浏览器和查看代码更改等操作。 许多开发人员更喜欢使用非调试模式快速启动应用并查看更改。

The default template creates RazorPagesMovie, Home, About and Contact links and pages. Depending on the size of your browser window, you might need to click the navigation icon to show the links.

Home or Index page

Test the links. The RazorPagesMovie and Home links go to the Index page. The About and Contact links go to the About and Contact pages, respectively.

Project files and folders

The following table lists the files and folders in the project. For this tutorial, the Startup.cs file is the most important to understand. You don't need to review each link provided below. The links are provided as a reference when you need more information on a file or folder in the project.

File or folderPurpose
wwwroot Contains static assets. See Static files.
Pages Folder for Razor Pages.
appsettings.json Configuration
Program.cs Configures the host of the ASP.NET Core app.
Startup.cs Configures services and the request pipeline. See Startup.

The Pages/Shared folder

The _Layout.cshtml file contains common HTML elements (script and stylesheet links) and sets the layout for the app. For example when you select RazorPagesMovie, Home, About or Contact, a common set of elements appears in the webpage. The common elements include the navigation menu at the top and the header at the bottom of the window. For more information, see Layout.

The _ValidationScriptsPartial.cshtml file provides a reference to jQuery validation scripts. When the Create and Edit pages are added later in the tutorial, the _ValidationScriptsPartial.cshtml file is used.

The _CookieConsentPartial.cshtml file provides a navigation bar and content to summarize the privacy and cookie use policy. For more information on the GDPR assets included in the project, see EU General Data Protection Regulation (GDPR) support in ASP.NET Core).

The Pages folder

The _ViewStart.cshtml sets the Razor Pages Layout property to use the _Layout.cshtml file. See Layout for more information.

The _ViewImports.cshtml file contains Razor directives that are imported into each Razor Page. See Importing Shared Directives for more information.

The AboutContact and Index pages are basic pages you can use to start an app. The Error page is used to display error information.

posted @ 2018-07-02 20:30  FH1004322  阅读(332)  评论(0)    收藏  举报