I'm trying to use HttpContext.Session.Get in my code and that gives me"ISession doesn't contain a definition for 'Get'" and the same for 'set' so I don't know what I miss
I am learning how to build simple e-commerce by getting a list of chosen products into an order report.
and I did use:
using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
but still, get the same error
controller
namespace EshopTest5.Controllers
{
public class OrderController : Controller
{
private readonly ApplicationDbContext _context;
private readonly IHttpContextAccessor _httpcontext;
public OrderController(ApplicationDbContext context, IHttpContextAccessor httpcontext)
{
_context = context;
_httpcontext = httpcontext;
}
// GET: OrderController/Checkout
public IActionResult Checkout()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Checkout(Order anOrder)
{
List<Product> products =_httpcontext. HttpContext.Session.Get<List<Product>>("products");
if (products != null)
{
foreach (var product in products)
{
OrderDetails orderDetails = new OrderDetails();
orderDetails.PorductId = product.ProductId;
anOrder.OrderDetails.Add(orderDetails);
}
}
anOrder.OrderNo = GetOrderNo();
_context.Orders.Add(anOrder);
await _context.SaveChangesAsync();
HttpContext.Session.Set("products", new List<Product>());
return View();
}
program:
using EshopTest5.Data;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(connectionString));
builder.Services.AddDatabaseDeveloperPageExceptionFilter();
builder.Services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<ApplicationDbContext>();
builder.Services.AddControllersWithViews();
builder.Services.AddHttpContextAccessor();
builder.Services.AddSession();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseMigrationsEndPoint();
}
else
{
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.UseSession();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.MapRazorPages();
app.Run();
CodePudding user response:
A generic Get<T>
method doesn't exist for ISession
, but there's an implementation of this as an extension-method in the docs that you can use:
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); } }
Add the preceding code to a SessionExtensions.cs
file, perhaps in the Microsoft.AspNetCore.Http
namespace.