I made a few changes to you source code but I do not have push rights so I cannot add a new branch.
The highlights are adding Session.
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorPages();
builder.Services.AddControllersWithViews();
builder.Services.AddDistributedMemoryCache();
builder.Services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromSeconds(10);
options.Cookie.HttpOnly = true;
options.Cookie.IsEssential = true;
});
builder.Services.AddDistributedMemoryCache();
builder.Services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromSeconds(10);
options.Cookie.HttpOnly = true;
options.Cookie.IsEssential = true;
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/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.UseSession();
app.MapRazorPages();
app.MapDefaultControllerRoute();
app.Run();
Saving data to Session
public async Task<IActionResult> ParamIndex(string path = "/locations/england/southwest/exmouth/", string apiKey = "")
{
ViewBag.SrvDtTime = DateTime.Now.ToString("MM/dd/yyyy HH:mm");
int franchiseId = 0;
int.TryParse(apiKey, out franchiseId);
FranchiseWidgetSettingVM? windgetSetting = null;
if (!string.IsNullOrWhiteSpace(path) || !string.IsNullOrWhiteSpace(apiKey))
{
windgetSetting = /*await*/ FindFranchiseWidgetSettings(franchiseId, 0, apiKey, string.Empty, path);
//Cache FranchiseWidgetSettingVM in Session
HttpContext.Session.Set<FranchiseWidgetSettingVM>("Data", windgetSetting ?? new FranchiseWidgetSettingVM());
}
windgetSetting = (windgetSetting != null ? windgetSetting : new FranchiseWidgetSettingVM());
ParamPageModel item = new ParamPageModel() { ApiKey = apiKey, Path = path, Data = windgetSetting };
return Redirect("/parambooking/?path=" + item.Path + "&apikey=" + item.ApiKey);
}
Simplifying the Razor Page
@page "/parambooking"
@model testapp.Pages.IndexModel
@{
DateTime curDateTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, 0);
DateTime serverStartDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.AddDays(5).Day, 9, 0, 0);
DateTime serverEndDate = serverStartDate.AddDays(1);
}
<div>
FranchiseName: @Model.ParamPageModel?.Data?.FranchiseName
</div>
<div>
UpdatedDate: @Model.ParamPageModel?.Data?.UpdatedDate
</div>
<div>
TransactionDate: @Model.ParamPageModel?.Data?.TransactionDate
</div>
<div>
CustomApiUrl: @Model.ParamPageModel?.Data?.CustomApiUrl
</div>
Code
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using testapp.Models;
using testapp.Extensions;
namespace testapp.Pages
{
public class IndexModel : PageModel
{
[BindProperty]
public ParamPageModel? ParamPageModel { get; set; }
public void OnGet(string path, string apiKey)
{
FranchiseWidgetSettingVM? vm = HttpContext.Session.Get<FranchiseWidgetSettingVM>("Data");
ParamPageModel = new Models.ParamPageModel() { Path = path, ApiKey = apiKey, Data = vm };
}
}
}
Moving ViewModels to the Models fodler
namespace testapp.Models
{
public class ParamPageModel
{
public string Path { get; set; } = "";
public string ApiKey { get; set; } = "";
public FranchiseWidgetSettingVM? Data { get; set; } = null!;
}
public class FranchiseWidgetSettingVM
{
public int Id { get; set; }
public int FranchiseId { get; set; }
public string? FranchiseName { get; set; }
public string ApiKey { get; set; } = null!;
public string WebSiteUrl { get; set; } = null!;
public string CustomApiUrl { get; set; } = null!;
public short StatusId { get; set; }
public DateTime? TransactionDate { get; set; }
public string? TransactionType { get; set; }
public DateTime? InsertedDate { get; set; }
public int? InsertedUserId { get; set; }
public string? InsertedUserName { get; set; }
public DateTime? UpdatedDate { get; set; }
public int? UpdateUserId { get; set; }
public string? UpdatedUserName { get; set; }
}
}
Using an extension method to handle serializing/deserializing session data.
using System.Text.Json;
namespace testapp.Extensions
{
public static class SessionExtensions
{
public static void Set<T>(this ISession session, string key, T value)
{
session.SetString(key, JsonSerializer.Serialize(value));
}
public static T? Get<T>(this ISession session, string key)
{
var value = session.GetString(key);
return value == null ? default : JsonSerializer.Deserialize<T>(value);
}
}
}
Results
FranchiseName: Fr1
UpdatedDate:
TransactionDate:
CustomApiUrl: /custom/contact/