I want to use Session in an action of my MVC controller but I encountered this error but I'm not sure why the error is coming out.
System.NullReferenceException: 'Object reference not set to an instance of an object.'
System.Web.HttpContext.Current.get returned null.
Controller:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using fyp.Models;
using System.Security.Claims;
using System.Data;
using System.Web;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
namespace fyp.Controllers
{
public class CustomerController : Controller
{
//Omitted actions
[HttpGet]
public IActionResult StartOrderMenu()
{
ViewData["Layout"] = "_Layout";
List<Cart> carts = new List<Cart>();
var jsoncart = JsonConvert.SerializeObject(carts);
System.Web.HttpContext.Current.Session["cart"] = jsoncart;
DbSet<Food> dbs = _dbContext.Food;
List<Food> model = dbs.ToList();
return View(model);
}
}
}