Skip to content

Latest commit

 

History

History
355 lines (193 loc) · 12.8 KB

razor-pages-start.md

File metadata and controls

355 lines (193 loc) · 12.8 KB
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

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

By Rick Anderson

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

[!INCLUDE]

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.

Home or Index page

Prerequisites

[!INCLUDE]

[!INCLUDE]

[!INCLUDE]


Create a Razor Pages web app

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

  • Create a new ASP.NET Core Web Application and select Next. 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 3.1 in the dropdown, Web Application, and then select Create.

new ASP.NET Core Web Application

The following starter project is created:

Solution Explorer

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

macOS New solution

  • Select .NET Core > App > Web Application > Next.

    macOS New project dialog

  • In the Configure your new Web Application dialog, set the Target Framework to .NET Core 3.1.

    macOS .NET Core 3.1 selection

  • Name the project RazorPagesMovie, and then select Create.

    nameproj


Run the app

[!INCLUDE]

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 xref:mvc/views/layout.

wwwroot folder

Contains static files, such as HTML files, JavaScript files, and CSS files. For more information, see xref:fundamentals/static-files.

appSettings.json

Contains configuration data, such as connection strings. For more information, see xref:fundamentals/configuration/index.

Program.cs

Contains the entry point for the program. For more information, see xref:fundamentals/host/generic-host.

Startup.cs

Contains code that configures app behavior. For more information, see xref:fundamentals/startup.

Next steps

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.

[!INCLUDE]

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.

Home or Index page

Prerequisites

[!INCLUDE]

[!INCLUDE]

[!INCLUDE]


Create a Razor Pages web app

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

  • Create a new ASP.NET Core Web Application and select Next.

    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, Web Application, and then select Create.

new ASP.NET Core Web Application

The following starter project is created:

Solution Explorer

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

macOS New solution

  • Select .NET Core > App > Web Application > Next.

    macOS New project dialog

  • In the Configure your new ASP.NET Core Web API dialog, set the Target Framework to .NET Core 3.1.

    macOS .NET Core 3.0 selection

  • Name the project RazorPagesMovie, and then select Create.

    nameproj


Run the app

  • Press Ctrl+F5 to run without the debugger.

    [!INCLUDE]

    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

[!INCLUDE]

  • 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 shows localhost:port# and not something like example.com. That's because localhost 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).

    Home or Index page

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

    Home or Index page

[!INCLUDE]

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

    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 xref:mvc/views/layout.

wwwroot folder

Contains static files, such as HTML files, JavaScript files, and CSS files. For more information, see xref:fundamentals/static-files.

appSettings.json

Contains configuration data, such as connection strings. For more information, see xref:fundamentals/configuration/index.

Program.cs

Contains the entry point for the program. For more information, see xref:fundamentals/host/generic-host.

Startup.cs

Contains code that configures app behavior, such as whether it requires consent for cookies. For more information, see xref:fundamentals/startup.

Additional resources

Next steps

Advance to the next tutorial in the series:

[!div class="step-by-step"] Add a model

::: moniker-end