建立 Razor web 應用程式Create Razor web apps

本指南提供建立您的第一個 Razor web 應用程式的簡介。This guide offers an introduction to creating your first Razor web app. 如需更深入的指引,請參閱 ASP.NET Core 中的 Razor Pages 簡介For more in-depth guidance, see Introduction to Razor Pages in ASP.NET Core.

Visual Studio for Mac 提供對 Razor 編輯的支援,包含 .cshtml 檔案中的 IntelliSense 和語法醒目提示。Visual Studio for Mac provides support for Razor editing, including IntelliSense and syntax highlighting in .cshtml files. 適用于 Mac 8.3 + 的 Visual Studio 2019 中的新功能,是在 Razor 檔案中具有內容感知 IntelliSense 的功能,因此您會收到符合您目前在檔中編輯之語言的 IntelliSense。New in Visual Studio 2019 for Mac 8.3+ is the ability to have context aware IntelliSense within a Razor file, so that you receive IntelliSense that matches the language you are currently editing within a document.

Visual Studio for Mac 中的 Razor 編輯

建立新的 Razor 專案Creating a new Razor project

  1. 在 [歡迎使用] 畫面上,選取 [ 新增 ] 以建立新的專案:On the welcome screen, select New to create a new project:

    Visual Studio for Mac [新增] 對話方塊

  2. 在 [新增專案] 對話方塊中,移至 .net Core > 應用 > 程式 Web 應用程式,然後選取 [下一步]In the New Project dialog box, go to .NET Core > App > Web Application and select Next:

    Razor 專案範本

  3. 選取您的 .NET Core 目標 framework (我們建議2.2 版或更新版本的) ,然後選取 [下一步]Select your .NET Core target framework (we recommend version 2.2 or later), and then select Next. 選擇專案的名稱,並視需要新增 Git 支援。Choose a name for your project, and add Git support if necessary. 選取 [Create] (建立) 以建立專案。Select Create to create the project.

    Razor 專案名稱

    Visual Studio for Mac 會在程式碼配置視窗中開啟您的專案。Visual Studio for Mac opens your project in the Code layout window.

  4. 使用 命令 + Option + F5來執行專案,而不需進行任何偵錯工具。Run the project without debugging by using Command+Option+F5.

    Visual Studio 開始 Kestrel、將瀏覽器開啟至 https://localhost:5001 ,並顯示您的第一個 Razor web 應用程式。Visual Studio starts Kestrel, opens a browser to https://localhost:5001, and displays your first Razor web app.

    Safari 中的 Razor Web 應用程式

專案剖析Project anatomy

Razor web 應用程式包含下列元件。Razor web apps include the following components.

Pages 資料夾Pages folder

此資料夾包含專案的網頁,以及每個專案的程式碼後端:This folder contains a project's webpages, along with the code-behind for each:

  • HTML 標籤和 Razor 語法的* * cshtml 檔案。*A *.cshtml file for the HTML markup and Razor syntax.
  • C # 程式碼後端用來處理頁面事件的* * cshtml.cs 檔案。*A *.cshtml.cs file for your C# code-behind for handling page events.

支援檔案的名稱以底線開頭。Supporting files have names that begin with an underscore. 例如,配置* _ cshtml*檔案會設定所有頁面通用的 UI 元素。For example, the _Layout.cshtml file configures UI elements common to all pages. 此檔案會設定頁面頂端的導覽功能表和底部的著作權注意事項。This file sets up the navigation menu at the top of the page and the copyright notice at the bottom. 如需詳細資訊,請參 ASP.NET 中的配置For more information, see Layout in ASP.NET Core.

啟動設定Launch settings

檔案 * 上的launchSettings.js* 包含 IIS 設定、應用程式 URL 和其他相關設定。The launchSettings.json file contains the IIS settings, the application URL, and other related settings.

應用程式設定App settings

檔案 appSettings.js 包含設定資料,例如連接字串。The appSettings.json file contains configuration data such as connection strings.

如需設定的詳細資訊,請參閱 ASP.NET 指南中的設定。For more information about configuration, see the Configuration in ASP.NET guide.

wwwroot 資料夾wwwroot folder

此資料夾包含靜態檔案,例如 HTML、JavaScript 和 CSS 檔案。This folder contains static files, such as HTML, JavaScript, and CSS files. 如需詳細資訊,請參閱 ASP.NET Core 中的靜態檔案For more information, see Static files in ASP.NET Core.

Program.csProgram.cs

此檔案包含程式的進入點。This file contains the entry point for the program. 如需詳細資訊,請參閱 ASP.NET Core Web 主機For more information, see ASP.NET Core Web Host.

Startup.csStartup.cs

此檔案包含設定應用程式行為的程式碼,例如應用程式是否需要對 cookie 的同意。This file contains code that configures app behavior, such as whether the app requires consent for cookies. 如需詳細資訊,請參閱 ASP.NET Core 中的應用程式啟動For more information, see App startup in ASP.NET Core.

另請參閱See also

如需建立 Razor web 應用程式的完整指南,請參閱 ASP.NET Core 中的 Razor Pages 簡介For a more comprehensive guide to creating Razor web apps, see Introduction to Razor Pages in ASP.NET Core.