In this article I will explain with an example, how to pass (send) data from Page Model to Razor Page using Session in ASP.Net Core Razor Pages.
Note: For enabling and configuring Session in ASP.Net Core Razor Pages, please refer my article ASP.Net Core Razor Pages: Enable Session.
 
 
Namespaces
You will need to import the following namespace.
using Microsoft.AspNetCore.Http;
 
 
Razor PageModel (Code-Behind)
The PageModel consists of following Handler method.
Handler method for handling GET operation
Inside this Handler method, Session object is set.
A string value is set in the Session object using the SetString method of the HttpContext.Session property in PageModel.
public class IndexModel : PageModel
{
    public void OnGet()
    {
        //Set value in Session object.
        HttpContext.Session.SetString("Message", "Hello MVC!");
    }
}
 
 
Razor Page (HTML)
Inside the Razor HTML Page, the IHttpContextAccessor Interface object is injected in order to access the Session and its functions inside the View.
Note: For configuring IHttpContextAccessor in ASP.Net Core Razor Pages, please refer my article Using HttpContext in ASP.Net Core Razor Pages.
 
The value from the Session object is displayed in View using the GetString method of the HttpContext.Session property.
@page
@using Microsoft.AspNetCore.Http
@inject IHttpContextAccessor Accessor
@model Display_Session_View_Core_Razor.Pages.IndexModel
@{
    Layout = null;
}
 
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <div>
        @Accessor.HttpContext.Session.GetString("Message")
    </div>
</body>
</html>
 
 
Screenshot
ASP.Net Core Razor Pages: Pass (Send) data from Page Model to Razor Page using Session
 
 
Downloads