How to create CRUD app in ASP.NET Core-Razor Pages

How to create CRUD app in ASP.NET Core-Razor Pages


3 min read

Intro :-

Razor Pages is a one more web application programming model of ASP-dot-NET Core. It removes much of the ceremony of ASP-dot-NET MVC by adopting a file-based routing approach. Each Razor Pages file found under the Pages directory correlate to endpoint. Razor Pages have an associated C# objected called the page model, which holds each page's behaviour. Additionally, each page works on the limited semantics of HTML, only supporting GET and POST methods.


You are going to create simple ASP-dot-NET Core - Razorpages app

Software Requirements:-

Ubuntu-20.04 +jit [linux-x86_64]
,NET Core 3.1.416
Sqlite 3.31.1




Knowledge of C# and text editor/visual studio code

Step 1:- Create project folder structure

$ dotnet new webapp -o AspStud
The dotnet new command creates a new Razor Pages project in the AspStud folder.

Step 2:- Run Initial project

comment the app.UseHttpsRedirection(); in Startup.cs file( for running on ubuntu )


give following command at terminal
$ dotnet build

$ dotnet run

Step 3:- Add Model class

Create folder Models
Add Stud.cs file


The Movie class contains: The ID field is required by the database for the primary key. [DataType(DataType.Date)]: The DataType attribute specifies the type of the data (Date).

Step 4:-Add NuGet packages

dotnet tool install --global dotnet-ef --version 3.1.9

dotnet tool install --global dotnet-aspnet-codegenerator --version 3.1.4

dotnet add package Microsoft.EntityFrameworkCore.Sqlite --version 3.1.9

dotnet add package Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore --version 3.1.9

dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design --version 3.1.4

dotnet add package Microsoft.EntityFrameworkCore.Design --version 3.1.9

dotnet add package Microsoft.EntityFrameworkCore.SqlServer --version 3.1.9

dotnet add package Microsoft.Extensions.Logging.Debug --version 3.1.9

Here is the complete AspStud.csporj file

<Project Sdk="Microsoft.NET.Sdk.Web">


    <PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="3.1.9" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.9">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.1.9" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.9" />
    <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="3.1.9" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.4" />


Step 5:- Add database context class

Create folder Data
Add AspStudContext.cs file


Step 6:- Add database connection string

add sqlite conntection string in appsetting.json file


Step 7:- Initial Migration

Run the following .NET Core CLI commands:

$dotnet ef migrations add InitialCreate
$ dotnet ef database update

The migrations command generates code to create the initial database schema. The schema is based on the model specified in DbContext. The InitialCreate argument is used to name the migrations.

Step 8:- Update the Startup.cs file



Step 9:- Run the completed project

& dotnet run



After performing above steps, you made a base ASP-dot-NET Core -Razorpages basic app. Also in this tutorial you have explored sqlite database connection string, migration, dbcontext class etc.Keep in mind that scaffolding command dotnet-aspnet-codegenerator razorpage -m AspStudContext -udl -outDir Pages/Studs --referenceScriptLibrariesis not working in dotnet CLI on ubuntu, scaffolding command work only on with visual studio on window.