Tutorial: Get started with Razor Pages in ASP.NET Core

By Rick Anderson

This is the first tutorial of a series. The series teaches the basics of building an ASP.NET Core Razor Pages web app.

For a more advanced introduction aimed at experienced developers, see Introduction to Razor Pages.

At the end of the series you'll have an app that manages a database of movies.

View or download sample code (how to download).

In this tutorial, you:

  • 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.

Home or Index page

Prerequisites

Create a Razor Pages web app

  • From the Visual Studio File menu, select New > Project.

  • Create a new ASP.NET Core Web Application. Name the project RazorPagesMovie. It's important to name the project RazorPagesMovie so the namespaces will match when you copy and paste code.

    new ASP.NET Core Web Application

  • Select ASP.NET Core 2.2 in the dropdown, and then select Web Application.

    new ASP.NET Core Web Application

    The following starter project is created:

    Solution Explorer

Run the app

  • Press Ctrl+F5 to run without the debugger.

    Visual Studio displays the following dialog:

    This project is configured to use SSL. To avoid SSL warnings in the browser you can choose to trust the self-signed certificate that IIS Express has generated. Would you like to trust the IIS Express SSL certificate?

    Select Yes if you trust the IIS Express SSL certificate.

    The following dialog is displayed:

    Security warning dialog

    Select Yes if you agree to trust the development certificate.

    See Trust the ASP.NET Core HTTPS development certificate for more information.

    Visual Studio starts IIS Express and runs the app. The address bar shows localhost:port# and not something like example.com. That's because localhost 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).

    Home or Index page

    The following image shows the app after you give consent to tracking:

    Home or Index page

Examine the project files

Here's an overview of the main project folders and files that you'll work with in later tutorials.

Pages folder

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 Layout in ASP.NET Core.

wwwroot folder

Contains static files, such as HTML files, JavaScript files, and CSS files. For more information, see Static files in ASP.NET Core.

appSettings.json

Contains configuration data, such as connection strings. For more information, see Configuration in ASP.NET Core.

Program.cs

Contains the entry point for the program. For more information, see ASP.NET Core Web Host.

Startup.cs

Contains code that configures app behavior, such as whether it requires consent for cookies. For more information, see App startup in ASP.NET Core.

Additional resources

Next steps

In this tutorial, you:

  • Created a Razor Pages web app.
  • Ran the app.
  • Examined the project files.

Advance to the next tutorial in the series: