Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

CRUD Operation with Razor Pages in ASP.Net Core 3.1

In this asp.net Core tutorial, you will learn crud operation with razor pages in asp.net core 3.1 by implementing a simple book list application project.

Introduction to ASP.Net Core

We will understand the basics of ASP.Net Core the reason behind its evolution followed by the new files and folder structure that have been introduced. Once we understand that then we will take a look at new concepts like middleware routing, tag helpers, razor syntax, and much more.

Now with the asp.net core, there are two ways to build a web application; the first approach is MVC application, and the second approach is razor features application.

We will be building projects in both the technologies to understand how the basics work in both the projects. We will implement the basic crud website which stands for Create, Read, Update, and Delete functionality. So we will understand how to connect with the database and play with entity framework in our ASP.Net Core web application.

  • Keep reading on Web API CRUD Operations using ASP.NET MVC and Entity Framework

CRUD Operation with Razor Pages in ASP.Net Core 3.1

In the following demonstration, CRUD operation in ASP.Net Core 3.1 you will learn Razer Pages, and how Page Model and Page View comes into the picture. Let’s understand the evolution of asp.net core using the below presentation.

Tools Needed

  • Visual Studio 2019,
  • .NET Core SDK 3.1,
  • SQL Server 2019 Developer Version,
  • SQL Server Management Studio.

Razor Project

In this section, we will be creating our very first project. It will be a razor project and then once we created we will take a look at the file and folders that are created with this. We will also understand how an ASP.Net Core application is started and what are the files that are involved.

Let’s get started by creating a new project from Visual Studio 2019.

Step1:

Step2:

Step3:

Razor Pages

  • Introduced in asp.net core 2.0.
  • Razor Pages is a new feature asp.net core mvc that makes coding page focused scenarios easier and more productive.
  • Razor pages are not for simple scenarios, everything that you can do with the MVC you can do by using Razor pages like Routing, Models, ActionResult, Tag Helpers, and so on.
  • Rager pages have two parts:
    • Rager Page (UI/View),
    • Razor Model (Contains Handlers).

Folder Structure of ASP.Net Core 3.1 Project:

Now start building all the functionalities in our project. First, we will add the model that we want to and push it to the database.  After that, we will perform CRUD operations on the book list. Find the below source code:-

Create Book Model

using System.ComponentModel.DataAnnotations;

namespace BookListRazor.Model
{
    public class Book
    {
        [Key]
        public int Id { get; set; }

        [Required]
        public string Name { get; set; }

        public string Author { get; set; }

        public string ISBN { get; set; }

    }
}

Add Connection String and Packages

Install the following packages:-

appsettings.json

{
  "ConnectionStrings": {
    "DefaultConnection": "Server=LAPTOP-61VNB63I\\SQLEXPRESS;Database=BookListRazorDb;Trusted_Connection=True;MultipleActiveResultSets=True"
  },

  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*"
}

Add Book Table To Database

using Microsoft.EntityFrameworkCore;

namespace BookListRazor.Model
{
    public class ApplicationDbContext : DbContext
    {
        public ApplicationDbContext(DbContextOptions options) : base(options)
        {

        }

        public DbSet Book { get; set; }
    }
}

In order to create the Database from the EF Model, you can execute the following commands from the Package Manager Console.

Add-Migration
Update-Database

Startup.cs

using BookListRazor.Model;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace BookListRazor
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext(option => option.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
            services.AddControllersWithViews();
            services.AddRazorPages().AddRazorRuntimeCompilation();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
                endpoints.MapRazorPages();
            });
        }
    }
}

Download Source Code

Conclusion

I hope you liked this article on the crud operation in asp.net core 3.1. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

The post CRUD Operation with Razor Pages in ASP.Net Core 3.1 appeared first on DotNetTec.



This post first appeared on Asp Dot Net Tricks And Tips, Dot Net Coding Tips, Google Maps API Developer, please read the originial post: here

Share the post

CRUD Operation with Razor Pages in ASP.Net Core 3.1

×

Subscribe to Asp Dot Net Tricks And Tips, Dot Net Coding Tips, Google Maps Api Developer

Get updates delivered right to your inbox!

Thank you for your subscription

×