In this article I will explain with an example, how to use Stored Procedure in ASP.Net Core Razor Pages.
The Stored Procedure is called using the FromSql function of Entity Framework in ASP.Net Core Razor Pages.
 
 
Database
Here I am making use of Microsoft’s Northwind Database. You can download it from here.
 
 
Stored Procedure
You will need to create the following Stored Procedure in the Northwind Database. The Stored Procedure accepts @ContactName parameter which is used to perform a search on the records in Customers Table.
CREATE PROCEDURE Customers_SearchCustomers
    @ContactName NVARCHAR(30)
AS
BEGIN
    SET NOCOUNT ON;
    SELECT TOP 10 *
    FROM Customers
    WHERE ContactName LIKE '%' + @ContactName + '%'
END
 
 
Model
The Model class consists of the following four properties.
public class Customer
{
    public string CustomerID { get; set; }
    public string ContactName { get; set; }
    public string City { get; set; }
    public string Country { get; set; }
}
 
 
Database Context
Once the Entity Framework is configured and connected to the database table, the Database Context will look as shown below.
Note: For beginners in ASP.Net Core and Entity Framework, please refer my article ASP.Net Core Razor Pages: Simple Entity Framework Tutorial with example. It covers all the information needed for connecting and configuring Entity Framework with ASP.Net Core.
 
Inside the class, first inherit the EntityFrameworkCore namespace and then inherit the DbContext class.
Then using Dependency Injection, a Constructor is created DbContextOptions are passed as parameter and also the Constructor of base class i.e. DbContext class is inherited.
A DbSet Collection property of Customer Model is created, which will be later used for holding the Data fetched from SQL Server Database Table.
Finally, there is a method SearchCustomers which is used to call the Stored Procedure and return its result.
The Stored Procedure is called using the FromSql function. The FromSql function accepts the following two parameters:
1. SQL Query – SQL Query to execute the Stored Procedure.
2. Parameters – One or more objects of SqlParameter class.
Note: The FromSql function belongs to the Microsoft.EntityFrameworkCore namespace.
 
using System.Linq;
using System.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
 
namespace Search_EF_Razor_Core
{
    public class DBCtx : DbContext
    {
        public DBCtx(DbContextOptions<DBCtx> options) : base(options)
        {
        }
 
        public DbSet<Customer> Customers { get; set; }
 
        public IQueryable<Customer> SearchCustomers(string contactName)
        {
            SqlParameter pContactName = new SqlParameter("@ContactName", contactName);
            return this.Customers.FromSql("EXECUTE Customers_SearchCustomers @ContactName", pContactName);
        }
    }
}
 
 
Razor PageModel (Code-Behind)
The PageModel consists of following two Handler methods.
Handler method for handling GET operation
Inside this Handler method, the SearchCustomers method of the Database Context class is called and an empty string is passed as parameter.
Finally, the returned results are converted into Generic List collection and assigned to the public property Customers and returned to the Razor Page.
 
Handler method for handling POST operation
This Handler method is executed when the Search Button is clicked.
Inside this Handler method, the value of the Customer Name TextBox is submitted to this Handler method and the received value is passed as parameter to the SearchCustomers function.
Finally, the returned results are converted into Generic List collection and assigned to the public property Customers and returned to the Razor Page.
public class IndexModel : PageModel
{
    private DBCtx Context { get; }
 
    public IndexModel(DBCtx _context)
    {
        this.Context = _context;
    }
 
    public List<Customer> Customers { get; set; }
 
    public void OnGet()
    {
        this.Customers = this.Context.SearchCustomers("").ToList();
    }
 
    public void OnPostSearch(string customerName)
    {
        this.Customers = this.Context.SearchCustomers(customerName).ToList();
    }
}
 
 
Razor Page (HTML)
The Razor Page consists of an HTML Form with following ASP.Net Tag Helpers attributes, a HTML TextBox and a Submit Button.
method – It specifies the Form Method i.e. GET or POST. In this case it will be set to POST.
When the Search button is clicked, the Form is submitted.
Note: In the Razor PageModel, the Handler method name is OnPostSearch but here it will be specified as Search when calling from the Razor HTML Page.
 
For displaying the records, an HTML Table is used. A loop will be executed over the Model property which will generate the HTML Table rows with the Customer records.
@page
@model Search_EF_Razor_Core.Pages.IndexModel
@addTagHelper*, Microsoft.AspNetCore.Mvc.TagHelpers
 
@{
    Layout = null;
}
 
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <form method="post">
        <span>Customer Name:</span>
        <input type="text" name="CustomerName" />
        <input type="submit" value="Search" asp-page-handler="Search" />
        <br /><br />
        <table cellpadding="0" cellspacing="0">
            <tr>
                <th>Customer Id</th>
                <th>Contact Name</th>
                <th>City</th>
                <th>Country</th>
            </tr>
            @foreach (var customer in Model.Customers)
            {
                <tr>
                    <td>@customer.CustomerID</td>
                    <td>@customer.ContactName</td>
                    <td>@customer.City</td>
                    <td>@customer.Country</td>
                </tr>
            }
        </table>
    </form>
</body>
</html>
 
 
Screenshots
HTML Table displaying Top 10 records
Using Stored Procedure in ASP.Net Core Razor Pages
 
HTML Table displaying Filtered records
Using Stored Procedure in ASP.Net Core Razor Pages
 
 
Downloads