Kurz: Začínáme s Razor Pages v ASP.NET CoreTutorial: Get started with Razor Pages in ASP.NET Core
Podle Rick AndersonBy Rick Anderson
Toto je první kurz série, který se učí základy vytváření ASP.NET Core webové aplikace Razor Pages.This is the first tutorial of a series that teaches the basics of building an ASP.NET Core Razor Pages web app.
V rámci pokročilejšího úvodu zaměřeného na vývojáře, kteří jsou obeznámeni s řadiči a zobrazeními, najdete informace v tématu Úvod do Razor Pages.For a more advanced introduction aimed at developers who are familiar with controllers and views, see Introduction to Razor Pages.
Na konci série budete mít aplikaci, která spravuje databázi filmů.At the end of the series, you'll have an app that manages a database of movies.
V tomto kurzu se naučíte:In this tutorial, you:
- Vytvořte Razor Pages webovou aplikaci.Create a Razor Pages web app.
- Spusťte aplikaci.Run the app.
- Prověřte soubory projektu.Examine the project files.
Na konci tohoto kurzu budete mít funkční Razor Pages webovou aplikaci, kterou vytvoříte v pozdějších kurzech.At the end of this tutorial, you'll have a working Razor Pages web app that you'll build on in later tutorials.
PožadavkyPrerequisites
- Visual StudioVisual Studio
- Visual Studio CodeVisual Studio Code
- Visual Studio pro MacVisual Studio for Mac
- Visual Studio 2019 16,4 nebo novější s úlohou vývoje ASP.NET a webuVisual Studio 2019 16.4 or later with the ASP.NET and web development workload
Vytvoření webové aplikace v Razor PagesCreate a Razor Pages web app
- Visual StudioVisual Studio
- Visual Studio CodeVisual Studio Code
- Visual Studio pro MacVisual Studio for Mac
Ze sady Visual Studio souboru nabídce vyberte možnost nový > projektu.From the Visual Studio File menu, select New > Project.
Vytvořte novou ASP.NET Core webovou aplikaci a vyberte Další.Create a new ASP.NET Core Web Application and select Next.
Pojmenujte projekt RazorPagesMovie.Name the project RazorPagesMovie. Je důležité pojmenovat projekt RazorPagesMovie , aby se obory názvů shodovaly při kopírování a vkládání kódu.It's important to name the project RazorPagesMovie so the namespaces will match when you copy and paste code.
V rozevíracím seznamu vyberte položku ASP.NET Core 3,1 a potomvyberte vytvořit.Select ASP.NET Core 3.1 in the dropdown, Web Application, and then select Create.
Vytvoří se následující počáteční projekt:The following starter project is created:
Spuštění aplikaceRun the app
- Visual StudioVisual Studio
- Visual Studio CodeVisual Studio Code
- Visual Studio pro MacVisual Studio for Mac
Stisknutím kombinace kláves CTRL + F5 spustíte bez ladicího programu.Press Ctrl+F5 to run without the debugger.
Visual Studio zobrazí následující dialog:Visual Studio displays the following dialog:
Vyberte Ano , pokud důvěřujete IIS Express certifikát SSL.Select Yes if you trust the IIS Express SSL certificate.
Zobrazí se následující dialogové okno:The following dialog is displayed:
Pokud souhlasíte s tím, že certifikát pro vývoj důvěřujete, vyberte Ano .Select Yes if you agree to trust the development certificate.
Visual Studio spustí IIS Express a spustí aplikaci.Visual Studio starts IIS Express and runs the app. Panel Adresa zobrazuje
localhost:port#
a ne něco jakoexample.com
.The address bar showslocalhost:port#
and not something likeexample.com
. Důvodem je, želocalhost
je standardní název hostitele pro místní počítač.That's becauselocalhost
is the standard hostname for the local computer. Localhost obsluhuje pouze webové požadavky z místního počítače.Localhost only serves web requests from the local computer. Když Visual Studio vytvoří webový projekt, pro webový server se použije náhodný port.When Visual Studio creates a web project, a random port is used for the web server.
Prověřte soubory projektuExamine the project files
Tady je přehled hlavních složek projektu a souborů, se kterými budete pracovat v pozdějších kurzech.Here's an overview of the main project folders and files that you'll work with in later tutorials.
Složka stránkyPages folder
Obsahuje stránky Razor a podpůrné soubory.Contains Razor pages and supporting files. Každá stránka Razor Page je dvojicí souborů:Each Razor page is a pair of files:
- Soubor . cshtml , který obsahuje značku HTML s C# kódem pomocí syntaxe Razor.A .cshtml file that contains HTML markup with C# code using Razor syntax.
- Soubor . cshtml.cs obsahující C# kód, který zpracovává události stránky.A .cshtml.cs file that contains C# code that handles page events.
Podpůrné soubory mají názvy začínající podtržítkem.Supporting files have names that begin with an underscore. Například soubor _Layout. cshtml nakonfiguruje prvky uživatelského rozhraní společné pro všechny stránky.For example, the _Layout.cshtml file configures UI elements common to all pages. Tento soubor nastaví navigační nabídku v horní části stránky a oznámení o autorských právech v dolní části stránky.This file sets up the navigation menu at the top of the page and the copyright notice at the bottom of the page. Další informace najdete v tématu Rozložení v ASP.NET Core.For more information, see Rozložení v ASP.NET Core.
Složka wwwrootwwwroot folder
Obsahuje statické soubory, jako jsou soubory HTML, JavaScriptové soubory a soubory CSS.Contains static files, such as HTML files, JavaScript files, and CSS files. Další informace najdete v tématu Statické soubory v ASP.NET Core.For more information, see Statické soubory v ASP.NET Core.
appSettings.jsonappSettings.json
Obsahuje konfigurační data, například připojovací řetězce.Contains configuration data, such as connection strings. Další informace najdete v tématu Konfigurace v ASP.NET Core.For more information, see Konfigurace v ASP.NET Core.
Program.csProgram.cs
Obsahuje vstupní bod pro program.Contains the entry point for the program. Další informace najdete v tématu Obecný hostitel .NET.For more information, see Obecný hostitel .NET.
Startup.csStartup.cs
Obsahuje kód, který konfiguruje chování aplikace.Contains code that configures app behavior. Další informace najdete v tématu Spuštění aplikace v ASP.NET Core.For more information, see Spuštění aplikace v ASP.NET Core.
Další krokyNext steps
Přejděte k dalšímu kurzu v řadě:Advance to the next tutorial in the series:
Toto je první kurz řady.This is the first tutorial of a series. Série se učí základy vytváření ASP.NET Core webové aplikace Razor Pages.The series teaches the basics of building an ASP.NET Core Razor Pages web app.
V rámci pokročilejšího úvodu zaměřeného na vývojáře, kteří jsou obeznámeni s řadiči a zobrazeními, najdete informace v tématu Úvod do Razor Pages.For a more advanced introduction aimed at developers who are familiar with controllers and views, see Introduction to Razor Pages.
Na konci série budete mít aplikaci, která spravuje databázi filmů.At the end of the series, you'll have an app that manages a database of movies.
V tomto kurzu se naučíte:In this tutorial, you:
- Vytvořte Razor Pages webovou aplikaci.Create a Razor Pages web app.
- Spusťte aplikaci.Run the app.
- Prověřte soubory projektu.Examine the project files.
Na konci tohoto kurzu budete mít funkční Razor Pages webovou aplikaci, kterou vytvoříte v pozdějších kurzech.At the end of this tutorial, you'll have a working Razor Pages web app that you'll build on in later tutorials.
PožadavkyPrerequisites
- Visual StudioVisual Studio
- Visual Studio CodeVisual Studio Code
- Visual Studio pro MacVisual Studio for Mac
- Visual Studio 2019 s vývoj pro ASP.NET a web pracovního vytíženíVisual Studio 2019 with the ASP.NET and web development workload
- .NET core SDK 2.2 nebo vyšší.NET Core SDK 2.2 or later
Varování
Pokud používáte Visual Studio 2017, přečtěte si dotnet/sdk problém #3124 informace o verzích .NET Core SDK, které nefungují s Visual Studio.If you use Visual Studio 2017, see dotnet/sdk issue #3124 for information about .NET Core SDK versions that don't work with Visual Studio.
Vytvoření webové aplikace v Razor PagesCreate a Razor Pages web app
- Visual StudioVisual Studio
- Visual Studio CodeVisual Studio Code
- Visual Studio pro MacVisual Studio for Mac
Ze sady Visual Studio souboru nabídce vyberte možnost nový > projektu.From the Visual Studio File menu, select New > Project.
Vytvořte novou ASP.NET Core webovou aplikaci a vyberte Další.Create a new ASP.NET Core Web Application and select Next.
Pojmenujte projekt RazorPagesMovie.Name the project RazorPagesMovie. Je důležité pojmenovat projekt RazorPagesMovie , aby se obory názvů shodovaly při kopírování a vkládání kódu.It's important to name the project RazorPagesMovie so the namespaces will match when you copy and paste code.
V rozevíracím seznamu vyberte položku ASP.NET Core 2,2 a potomvyberte vytvořit.Select ASP.NET Core 2.2 in the dropdown, Web Application, and then select Create.
Vytvoří se následující počáteční projekt:The following starter project is created:
Spuštění aplikaceRun the app
- Visual StudioVisual Studio
- Visual Studio CodeVisual Studio Code
- Visual Studio pro MacVisual Studio for Mac
Stisknutím kombinace kláves CTRL + F5 spustíte bez ladicího programu.Press Ctrl+F5 to run without the debugger.
Visual Studio zobrazí následující dialog:Visual Studio displays the following dialog:
Vyberte Ano , pokud důvěřujete IIS Express certifikát SSL.Select Yes if you trust the IIS Express SSL certificate.
Zobrazí se následující dialogové okno:The following dialog is displayed:
Pokud souhlasíte s tím, že certifikát pro vývoj důvěřujete, vyberte Ano .Select Yes if you agree to trust the development certificate.
Visual Studio spustí IIS Express a spustí aplikaci.Visual Studio starts IIS Express and runs the app. V adresním řádku se zobrazí
localhost:port#
a ne něco jakoexample.com
.The address bar showslocalhost:port#
and not something likeexample.com
. Důvodem je, želocalhost
je standardní název hostitele pro místní počítač.That's becauselocalhost
is the standard hostname for the local computer. Localhost obsluhuje pouze webové požadavky z místního počítače.Localhost only serves web requests from the local computer. Když Visual Studio vytvoří webový projekt, použije se pro webový server náhodný port.When Visual Studio creates a web project, a random port is used for the web server.Na domovské stránce aplikace vyberte přijmout pro vyjádření souhlasu se sledováním.On the app's home page, select Accept to consent to tracking.
Tato aplikace nesleduje osobní údaje, ale šablona projektu zahrnuje funkci souhlasu pro případ, že ji budete potřebovat pro splnění Obecné nařízení o ochraně osobních údajů Evropské unie (GDPR).This app doesn't track personal information, but the project template includes the consent feature in case you need it to comply with the European Union's General Data Protection Regulation (GDPR).
Následující obrázek zobrazí aplikaci po poskytnutí souhlasu se sledováním:The following image shows the app after you give consent to tracking:
Prověřte soubory projektuExamine the project files
Tady je přehled hlavních složek projektu a souborů, se kterými budete pracovat v pozdějších kurzech.Here's an overview of the main project folders and files that you'll work with in later tutorials.
Složka stránkyPages folder
Obsahuje stránky Razor a podpůrné soubory.Contains Razor pages and supporting files. Každá stránka Razor Page je dvojicí souborů:Each Razor page is a pair of files:
- Soubor . cshtml , který obsahuje značku HTML s C# kódem pomocí syntaxe Razor.A .cshtml file that contains HTML markup with C# code using Razor syntax.
- Soubor . cshtml.cs obsahující C# kód, který zpracovává události stránky.A .cshtml.cs file that contains C# code that handles page events.
Podpůrné soubory mají názvy začínající podtržítkem.Supporting files have names that begin with an underscore. Například soubor _Layout. cshtml nakonfiguruje prvky uživatelského rozhraní společné pro všechny stránky.For example, the _Layout.cshtml file configures UI elements common to all pages. Tento soubor nastaví navigační nabídku v horní části stránky a oznámení o autorských právech v dolní části stránky.This file sets up the navigation menu at the top of the page and the copyright notice at the bottom of the page. Další informace najdete v tématu Rozložení v ASP.NET Core.For more information, see Rozložení v ASP.NET Core.
Složka wwwrootwwwroot folder
Obsahuje statické soubory, jako jsou soubory HTML, JavaScriptové soubory a soubory CSS.Contains static files, such as HTML files, JavaScript files, and CSS files. Další informace najdete v tématu Statické soubory v ASP.NET Core.For more information, see Statické soubory v ASP.NET Core.
appSettings.jsonappSettings.json
Obsahuje konfigurační data, například připojovací řetězce.Contains configuration data, such as connection strings. Další informace najdete v tématu Konfigurace v ASP.NET Core.For more information, see Konfigurace v ASP.NET Core.
Program.csProgram.cs
Obsahuje vstupní bod pro program.Contains the entry point for the program. Další informace najdete v tématu Obecný hostitel .NET.For more information, see Obecný hostitel .NET.
Startup.csStartup.cs
Obsahuje kód, který konfiguruje chování aplikace, například zda vyžaduje souhlas s soubory cookie.Contains code that configures app behavior, such as whether it requires consent for cookies. Další informace najdete v tématu Spuštění aplikace v ASP.NET Core.For more information, see Spuštění aplikace v ASP.NET Core.
Další materiály a zdroje informacíAdditional resources
Další krokyNext steps
Přejděte k dalšímu kurzu v řadě:Advance to the next tutorial in the series:
Váš názor
Načítání názorů...