In this article I will explain with an example, how to use Dapper library in ASP.Net Core (.Net Core) Razor Pages.
Note: For beginners in ASP.Net Core (.Net Core 7) Razor Pages, please refer my article ASP.Net Core 7 Razor Pages: Hello World Tutorial with Sample Program example.
 
 

What is Dapper?

Dapper is a simple micro-ORM used to simplify working with ADO.Net.
Dapper is an open-source object-relational mapping (ORM) library for .NET and .NET Core applications. This library allows developers quickly and easily access data from databases.
Dapper allows you to execute raw SQL queries, map the results to objects and execute Stored Procedure.
Dapper is lightweight and fast, making it an ideal choice for applications that require low latency and high performance.
It has support for both asynchronous and synchronous database queries and batching multiple queries together into a single call.
Additionally, dapper supports parameterized queries to help protect against SQL injection attacks.
 
 

Installing Dapper package using Nuget

In order to install Dapper library using Nuget, please refer my article Install Dapper from Nuget in Visual Studio.
 
 

Database

I have made use of the following table Customers with the schema as follows.
Using Dapper in ASP.Net Core Razor Pages
 
I have already inserted few records in the table.
Using Dapper in ASP.Net Core Razor Pages
 
Note: You can download the database table SQL by clicking the download link below.
          Download SQL file
 
 

Model

The Model class consists of following properties.
public class CustomerModel
{
    public int CustomerId { get; set; }
    public string Name { get; set; }
    public string Country { get; set; }
}
 
 

Namespaces

You will need to import the following namespaces.
using Dapper;
using System.Data.SqlClient;
 
 

Razor PageModel (Code-Behind)

The PageModel consists of following Handler method.

Handler Method for handling GET operation

Inside this Handler method, first the connection is read from the ConnectionStrings section of the AppSettings.json file.
Note: For more details on reading connection string from AppSettings.json file, please refer my article .Net Core 7: Read Connection String from AppSettings.json file.
 
Then, a connection to the database is established using the SqlConnection class.
Finally, using Query method of Dapper library records are fetched from the SQL Server database and assigned to the public property Customer.
public class IndexModel : PageModel
{
    public IConfiguration Configuration { get; set; }
 
    public List<CustomerModel> Customer { get; set; }
 
    public IndexModel(IConfiguration configuration)
    {
        this.Configuration = configuration;
    }
 
    public void OnGet()
    {
        string sql = "SELECT CustomerId, Name, Country FROM Customers";
        string constr = this.Configuration.GetSection("ConnectionStrings")["MyConn"];
        using (SqlConnection con = new SqlConnection(constr))
        {
            this.Customer = con.Query<CustomerModel>(sql).ToList();
        }
    }
}
 
 

Razor Page (HTML)

HTML Markup

The HTML of Razor Page consists of an HTML Table for displaying the records.
Then, a FOR EACH loop will be executed over the Model property which will generate the HTML Table rows with the Customer records.
@page
@using Dapper_Select_Core_Razor.Models
@model Dapper_Select_Core_Razor.Pages.IndexModel
@{
      Layout = null;
}
 
<!DOCTYPE html>
 
<html>
<head>
      <metaname="viewport"content="width=device-width" />
      <title>Index</title>
</head>
<body>
      <table cellpadding="0" cellspacing="0">
          <tr>
              <th>Customer Id</th>
              <th>Name</th>
              <th>Country</th>
          </tr>
          @foreach (CustomerModel customer in Model.Customer)
          {
              <tr>
                  <td>@customer.CustomerId</td>
                  <td>@customer.Name</td>
                  <td>@customer.Country</td>
             </tr>
          }
      </table>
</body>
</html>
 
 

Screenshot

Using Dapper in ASP.Net Core Razor Pages
 
 

Downloads