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.

Task:-

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

Level:-

Beginner

Prerequisite:-

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 )

asprazor4.png

give following command at terminal
$ dotnet build

$ dotnet run

Step 3:- Add Model class

Create folder Models
Add Stud.cs file

asprazor1.png

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">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <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>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <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" />
  </ItemGroup>

</Project>

Step 5:- Add database context class

Create folder Data
Add AspStudContext.cs file

asprazor2.png

Step 6:- Add database connection string

add sqlite conntection string in appsetting.json file

asprazor9.png

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

asprazor5.png

asprazor6.png

Step 9:- Run the completed project

& dotnet run

asprazor7.png

Conclusion:-

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.