title | author | description | ms.author | ms.date | uid |
---|---|---|---|---|---|
Tutorial: Get started with Razor Pages in ASP.NET Core |
rick-anderson |
This series of tutorials shows how to use Razor Pages in ASP.NET Core. Learn how to create a model, generate code for Razor pages, use Entity Framework Core and SQL Server for data access, add search functionality, add input validation, and use migrations to update the model. |
riande |
11/12/2019 |
tutorials/razor-pages/razor-pages-start |
::: moniker range=">= aspnetcore-3.0" This is the first tutorial of a series that teaches the basics of building an ASP.NET Core Razor Pages web app.
At the end of the series, you'll have an app that manages a database of movies.
[!INCLUDEView or download sample code]
In this tutorial, you:
[!div class="checklist"]
- Create a Razor Pages web app.
- Run the app.
- Examine the project files.
At the end of this tutorial, you'll have a working Razor Pages web app that you'll build on in later tutorials.
-
From the Visual Studio File menu, select New > Project.
-
Name the project RazorPagesMovie. It's important to name the project RazorPagesMovie so the namespaces will match when you copy and paste code.
-
Select ASP.NET Core 3.1 in the dropdown, Web Application, and then select Create.
The following starter project is created:
-
Open the integrated terminal.
-
Change to the directory (
cd
) which will contain the project. -
Run the following commands:
dotnet new webapp -o RazorPagesMovie code -r RazorPagesMovie
- The
dotnet new
command creates a new Razor Pages project in the RazorPagesMovie folder. - The
code
command opens the RazorPagesMovie folder in the current instance of Visual Studio Code.
- The
-
After the status bar's OmniSharp flame icon turns green, a dialog asks Required assets to build and debug are missing from 'RazorPagesMovie'. Add them? Select Yes.
A .vscode directory, containing launch.json and tasks.json files, is added to the project's root directory.
- Select File > New Solution.
-
Select .NET Core > App > Web Application > Next.
-
In the Configure your new Web Application dialog, set the Target Framework to .NET Core 3.1.
-
Name the project RazorPagesMovie, and then select Create.
Here's an overview of the main project folders and files that you'll work with in later tutorials.
Contains Razor pages and supporting files. Each Razor page is a pair of files:
- A .cshtml file that contains HTML markup with C# code using Razor syntax.
- A .cshtml.cs file that contains C# code that handles page events.
Supporting files have names that begin with an underscore. 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 of the page. For more information, see xref:mvc/views/layout.
Contains static files, such as HTML files, JavaScript files, and CSS files. For more information, see xref:fundamentals/static-files.
Contains configuration data, such as connection strings. For more information, see xref:fundamentals/configuration/index.
Contains the entry point for the program. For more information, see xref:fundamentals/host/generic-host.
Contains code that configures app behavior. For more information, see xref:fundamentals/startup.
Advance to the next tutorial in the series:
[!div class="step-by-step"] Add a model
::: moniker-end
::: moniker range="< aspnetcore-3.0"
This is the first tutorial of a series. The series teaches the basics of building an ASP.NET Core Razor Pages web app.
At the end of the series, you'll have an app that manages a database of movies.
[!INCLUDEView or download sample code]
In this tutorial, you:
[!div class="checklist"]
- Create a Razor Pages web app.
- Run the app.
- Examine the project files.
At the end of this tutorial, you'll have a working Razor Pages web app that you'll build on in later tutorials.
-
From the Visual Studio File menu, select New > Project.
-
Create a new ASP.NET Core Web Application and select Next.
-
Name the project RazorPagesMovie. It's important to name the project RazorPagesMovie so the namespaces will match when you copy and paste code.
-
Select ASP.NET Core 2.2 in the dropdown, Web Application, and then select Create.
The following starter project is created:
-
Open the integrated terminal.
-
Change to the directory (
cd
) which will contain the project. -
Run the following commands:
dotnet new webapp -o RazorPagesMovie code -r RazorPagesMovie
- The
dotnet new
command creates a new Razor Pages project in the RazorPagesMovie folder. - The
code
command opens the RazorPagesMovie folder in the current instance of Visual Studio Code.
- The
-
After the status bar's OmniSharp flame icon turns green, a dialog asks Required assets to build and debug are missing from 'RazorPagesMovie'. Add them? Select Yes.
A .vscode directory, containing launch.json and tasks.json files, is added to the project's root directory.
- Select File > New Solution.
-
Select .NET Core > App > Web Application > Next.
-
In the Configure your new ASP.NET Core Web API dialog, set the Target Framework to .NET Core 3.1.
-
Name the project RazorPagesMovie, and then select Create.
-
Press Ctrl+F5 to run without the debugger.
Visual Studio starts IIS Express and runs the app. The address bar shows
localhost:port#
and not something likeexample.com
. That's becauselocalhost
is the standard hostname for the local computer. Localhost only serves web requests from the local computer. When Visual Studio creates a web project, a random port is used for the web server. -
On the app's home page, select Accept to consent to tracking.
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).
The following image shows the app after you give consent to tracking:
-
Press Ctrl-F5 to run without the debugger.
Visual Studio Code starts Kestrel, launches a browser, and navigates to
http://localhost:5001
. The address bar showslocalhost:port#
and not something likeexample.com
. That's becauselocalhost
is the standard hostname for local computer. Localhost only serves web requests from the local computer. -
On the app's home page, select Accept to consent to tracking.
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).
The following image shows the app after you give consent to tracking:
-
Press Cmd-Opt-F5 to run without the debugger.
Visual Studio starts Kestrel, launches a browser, and navigates to
http://localhost:5001
. -
On the app's home page, select Accept to consent to tracking.
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).
The following image shows the app after you give consent to tracking:
Here's an overview of the main project folders and files that you'll work with in later tutorials.
Contains Razor pages and supporting files. Each Razor page is a pair of files:
- A .cshtml file that contains HTML markup with C# code using Razor syntax.
- A .cshtml.cs file that contains C# code that handles page events.
Supporting files have names that begin with an underscore. 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 of the page. For more information, see xref:mvc/views/layout.
Contains static files, such as HTML files, JavaScript files, and CSS files. For more information, see xref:fundamentals/static-files.
Contains configuration data, such as connection strings. For more information, see xref:fundamentals/configuration/index.
Contains the entry point for the program. For more information, see xref:fundamentals/host/generic-host.
Contains code that configures app behavior, such as whether it requires consent for cookies. For more information, see xref:fundamentals/startup.
Advance to the next tutorial in the series:
[!div class="step-by-step"] Add a model
::: moniker-end