Developing ASP.NET Core Project With VS Code – Razor Pages

ref: https://docs.microsoft.com/en-us/aspnet/core/tutorials/razor-pages/razor-pages-start?view=aspnetcore-2.2&tabs=visual-studio-code

Create a Razor Pages web app
Type in the terminal

Open it on VSCode

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.

Run the app
On the VSCode, open the terminal by pressing CTRL + ~
then type

NOTE: I FOUND THIS COMMAND: ‘dotnet dev-certs https –trust’ IS NOT WORKING
Then Press Ctrl-F5 to run without the debugger. It’ll open the browser automatically with this url: https://localhost:5001/

Add a data model
Add a folder named Models.
Add a class to the Models folder named Movie.cs.
Add the following properties to the Movie class:

Add a database context class
Add the following RazorPagesMovieContext class to the Data folder:

Add a database connection string
Add a connection string to the appsettings.json file as shown in the following highlighted code:

Add required NuGet packages
Run the following .NET Core CLI command to add SQLite and CodeGeneration.Design to the project:

The Microsoft.VisualStudio.Web.CodeGeneration.Design package is required for scaffolding.

Register the database context
Add the following using statements at the top of Startup.cs:

Register the database context with the dependency injection container in Startup.cs in ConfigureServices function:

Build the project (‘dotnet build’) to verify there are no compilation errors.

 

Scaffold the movie model
In this section, the movie model is scaffolded. That is, the scaffolding tool produces pages for Create, Read, Update, and Delete (CRUD) operations for the movie model.
Install the scaffolding tool:

Run the scaffolding tool to create CRUD for model ‘Movie’:

The scaffold process creates and updates the following files:

Files created
Pages/Movies: Create, Delete, Details, Edit, and Index.
Data/RazorPagesMovieContext.cs
File updated
Startup.cs
The created and updated files are explained in the next section.

 

Leave a Reply

Your email address will not be published. Required fields are marked *