简体   繁体   English

System.Web.HttpContext.Current.get在ASP.NET MVC控制器中返回null

[英]System.Web.HttpContext.Current.get returned null in asp.net mvc controller

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. 我想在我的MVC控制器的操作中使用Session,但是遇到此错误,但是我不确定为什么会出现此错误。

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);
        }
    }
}

The System.Web namespace is not used in ASP.NET Core MVC, hence System.Web.HttpContext.Current property will always return null value (note that HttpContext is injected). ASP.NET Core MVC中未使用System.Web命名空间,因此System.Web.HttpContext.Current属性将始终返回null值(请注意,已注入HttpContext )。 If you want to set session variable in IActionResult controller, you can use SessionExtensions.SetString method: 如果要在IActionResult控制器中设置会话变量, IActionResult可以使用SessionExtensions.SetString方法:

string key = "cart";

HttpContext.Session.SetString(key, jsoncart);

If you want to retrieve it, use SessionExtensions.GetString method: 如果要检索它,请使用SessionExtensions.GetString方法:

string jsonCart = HttpContext.Session.GetString("cart");

Note: To get HttpContext instance work, put using Microsoft.AspNetCore.Http; 注意:要获得HttpContext实例的工作,请using Microsoft.AspNetCore.Http; before namespace declaration. 在命名空间声明之前。

Further reference: Session and app state in ASP.NET Core 进一步参考: ASP.NET Core中的会话和应用程序状态

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将ASP.NET MVC HttpContext发送到Web Api HttpContext - Send ASP.NET MVC HttpContext to Web Api HttpContext ASP.NET中的System.Web.HttpContext.Current.User.Identity.Name与System.Environment.UserName的对比 - System.Web.HttpContext.Current.User.Identity.Name Vs System.Environment.UserName in ASP.NET 鉴于一台服务器上的ASP.Net MVC站点而不是另一台服务器上的HttpContext.Current.User.Identity为空/空 - HttpContext.Current.User.Identity is null/empty in view of ASP.Net MVC site on one server but not the other ASP.Net MVC 5-在控制器外部访问HttpContext.Current.User的正确方法? - ASP.Net MVC 5 - Correct way to access HttpContext.Current.User outside of controller? 在ASP.NET Core中引用使用System.Web.HttpContext.Current的ASP.NET Framework 4.6.2 - Referencing ASP.NET framework 4.6.2 in ASP.NET Core, Which use System.Web.HttpContext.Current HttpContext.Current.Request.UrlReferrer 始终为 null ASP.NET MVC - HttpContext.Current.Request.UrlReferrer is always null ASP.NET MVC ASP.NET Core 3.1,从当前HttpContext中的referrer url获取controller和动作名称 - ASP.NET Core 3.1, get the controller and action name from referrer url in the current HttpContext Asp.net HttpContext.Current.Items返回null - Asp.net HttpContext.Current.Items returning null HttpContext.Current.Features.Get<IRequestCultureFeature> () 在 asp.net Core 中为 null - HttpContext.Current.Features.Get<IRequestCultureFeature>() is null in asp.net Core 如何从 ASP.NET mvc 中的 controller 访问 HttpContext - How to access HttpContext from controller in ASP.NET mvc
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM