ASP.NET CORE 8 RAZOR PAGES — CREATING THE BASIC PROJECT
ASP.NET CORE 8 RAZOR PAGES — CREATING THE DOTNETEF PROJECT
This tutorial creates the basic dotnetef ASP.NET Core 8 Razor Pages application that will be used as the starting point for the Entity Framework Core series. The project is first created, built and tested before EF Core and SQLite are introduced.
1. What will we build?
Before working with Entity Framework Core, we need a working ASP.NET Core Razor Pages application.
In this tutorial, we will create a project named:
dotnetef
The project will become the foundation for the Entity Framework Core and SQLite tutorials that follow.
The overall learning sequence will be:
PART 0
Create ASP.NET Core 8 Razor Pages
↓
dotnetef works
↓
PART 1
EF Core + SQLite
↓
Product model + DbContext
↓
Migration
↓
products.db
↓
PART 2
Product CRUD
↓
Create / Read / Update / Delete
2. Prerequisites
Before creating the project, make sure the .NET 8 SDK is installed on your computer.
Open a terminal or command prompt and run:
dotnet --version
The result should show a .NET 8 SDK version, for example:
8.0.xxx
You can also display all installed .NET SDK versions:
dotnet --list-sdks
3. Create a project directory
Choose a directory where you want to store your .NET projects.
For example:
mkdir dotnet-projects
cd dotnet-projects
On Windows Command Prompt, you can use the same commands:
mkdir dotnet-projects
cd dotnet-projects
4. Create the Razor Pages project
Use the dotnet new webapp command to create an ASP.NET Core Razor Pages application.
dotnet new webapp -n dotnetef --framework net8.0
The command has three important parts:
| Part | Purpose |
|---|---|
dotnet new webapp |
Creates an ASP.NET Core Razor Pages application. |
-n dotnetef |
Names the project dotnetef. |
--framework net8.0 |
Targets the .NET 8 framework. |
5. Enter the project directory
After creating the project, move into the project directory:
cd dotnetef
List the project files.
On Windows:
dir
On Linux or macOS:
ls
You should see files and directories similar to:
dotnetef/
├── Pages/
├── Properties/
├── wwwroot/
├── appsettings.json
├── appsettings.Development.json
├── dotnetef.csproj
└── Program.cs
6. Understand the initial project structure
The newly created application contains several important directories and files.
| Item | Purpose |
|---|---|
Pages/ |
Contains Razor Pages and their page models. |
wwwroot/ |
Contains static files such as CSS, JavaScript and images. |
Properties/ |
Contains development and launch configuration. |
Program.cs |
Configures and starts the ASP.NET Core application. |
appsettings.json |
Contains application configuration settings. |
dotnetef.csproj |
Contains project configuration and package information. |
7. Examine the Razor Pages
The Pages directory contains the pages generated by the Razor Pages template.
The structure should be similar to:
Pages/
├── Error.cshtml
├── Error.cshtml.cs
├── Index.cshtml
├── Index.cshtml.cs
├── Privacy.cshtml
├── Privacy.cshtml.cs
├── Shared/
├── _ViewImports.cshtml
└── _ViewStart.cshtml
The main pages include:
| Page | Purpose |
|---|---|
Index.cshtml |
The application's home page. |
Privacy.cshtml |
The example Privacy page. |
Error.cshtml |
Displays application error information. |
Shared/ |
Contains shared Razor layout components. |
8. Examine Program.cs
The generated Program.cs contains the basic configuration required for a Razor Pages application.
It should have a structure similar to:
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddRazorPages();
var app = builder.Build();
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapRazorPages();
app.Run();
The important statement for Razor Pages is:
builder.Services.AddRazorPages();
This registers Razor Pages services with the ASP.NET Core dependency injection system.
The application is then configured to expose the Razor Pages using:
app.MapRazorPages();
9. Examine appsettings.json
The initial appsettings.json file contains general application configuration.
It should be similar to:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
At this stage, there is no database connection string.
In the EF Core tutorial, this file will later be changed to include:
"ConnectionStrings": {
"ProductDatabase": "Data Source=Data/products.db"
}
This will allow the application to connect to the SQLite database created in Part 1.
10. Examine the project file
Open:
dotnetef.csproj
The project file should contain configuration similar to:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
</Project>
Notice that EF Core packages are not yet present.
Later, the EF Core tutorial will add packages such as:
Microsoft.EntityFrameworkCore.Sqlite
Microsoft.EntityFrameworkCore.Design
11. Build the project
Before running the application, compile the project:
dotnet build
A successful build should produce output similar to:
Build succeeded.
0 Warning(s)
0 Error(s)
12. Run the application
Start the application with:
dotnet run
The terminal should display addresses similar to:
Now listening on: https://localhost:7xxx
Now listening on: http://localhost:5xxx
The actual port numbers may be different on your computer.
Open the HTTPS address in a web browser.
13. Test the Home page
The default Razor Pages application should display the Home page.
Verify that the page loads without an error.
You should also be able to navigate to the Privacy page.
Home
Privacy
This confirms that the Razor Pages application is functioning before any database functionality is introduced.
14. Stop the application
Return to the terminal running the application and press:
Ctrl+C
The application will stop and return you to the command prompt.
15. Optional: Add existing features from the earlier series
If this project is being used as part of an earlier dotnetef tutorial series, make sure that any previously developed pages and features are also present.
For example, the completed project used by the EF Core tutorial may contain additional functionality such as:
- A server-time handler.
- A custom 404 page.
- Other Razor Pages developed in the earlier tutorials.
dotnetef project from the previous tutorial.
16. Final project structure
Before starting EF Core, the project should look approximately like this:
dotnetef/
│
├── Pages/
│ ├── Error.cshtml
│ ├── Error.cshtml.cs
│ ├── Index.cshtml
│ ├── Index.cshtml.cs
│ ├── Privacy.cshtml
│ ├── Privacy.cshtml.cs
│ ├── Shared/
│ ├── _ViewImports.cshtml
│ └── _ViewStart.cshtml
│
├── Properties/
│ └── launchSettings.json
│
├── wwwroot/
│ ├── css/
│ ├── js/
│ └── favicon.ico
│
├── appsettings.Development.json
├── appsettings.json
├── dotnetef.csproj
└── Program.cs
17. What should NOT exist yet?
At this stage, the following EF Core files should not yet exist:
Models/Product.cs
Data/ProductDbContext.cs
Data/products.db
Data/Migrations/
These files will be introduced in the next tutorial.
18. Final verification
Before moving to EF Core Part 1, perform the following checks:
| Check | Command | Expected result |
|---|---|---|
| Check .NET SDK | dotnet --version |
.NET 8 version is available |
| Build project | dotnet build |
Build succeeds |
| Run application | dotnet run |
Application starts |
| Test browser | Open localhost URL | Home page loads |
| Test Razor Pages | Open Privacy page | Privacy page loads |
What has been achieved?
- A new ASP.NET Core 8 Razor Pages project has been created.
- The project is named
dotnetef. - The project targets .NET 8.
- The Razor Pages application has been successfully built.
- The application has been successfully started.
- The Home and Privacy pages have been tested.
- The project is ready for Entity Framework Core.
PART 0 complete: The basic ASP.NET Core 8 Razor Pages application is now ready. No database or EF Core components have been added yet.
Next step
The next tutorial will add Entity Framework Core and SQLite to this application.
ASP.NET Core 8 Razor Pages
↓
dotnetef
↓
EF Core 8
↓
SQLite
↓
Product model
↓
ProductDbContext
↓
Migration
↓
products.db
Next: ENTITY FRAMEWORK CORE (EF CORE) — PART 1: SETTING UP EF CORE WITH SQLITE. We will install the EF Core packages, create the Product model and ProductDbContext, configure SQLite, generate the first migration and create the physical Data/products.db database.