IHttpContextAccessor 会话 GetString [英] IHttpContextAccessor Session GetString
问题描述
我正在尝试使用 .NET Core 运行时将 ASP.NET MVC 站点迁移到 ASP.NET Core.以前我们可以使用
I'm trying to migrate an ASP.NET MVC site to ASP.NET Core with the .NET Core runtime. Previously we could get objects out of the session store, even in different assemblies, with
var obj = HttpContext.Current.Session[key]
现在我读到我们必须注入 IHttpContextAccessor 并使用 _contextAccessor.HttpContext.Session 上的方法.但是 Session 对象的方法发生了变化,不再应用索引.
Now I've read we must inject IHttpContextAccessor and use the method on _contextAccessor.HttpContext.Session. But the methods of the Session object have changed, and it no longer has indexing applied.
我在其他问题中看到有人使用 HttpContext.Session.GetString() 和 HttpContext.Session.SetString():
I have seen people in other questions using HttpContext.Session.GetString() and HttpContext.Session.SetString():
有了这些,我至少可以反/序列化我想要获取/获取的对象.但是我在界面上找不到这些方法.
With these I could at least de/serialize the object I want to fetch/get. But I can't find these methods on the interface.
'ISession' 不包含 'GetString' 的定义,也没有扩展方法可以找到接受类型为ISession"的第一个参数的GetString"
'ISession' does not contain a definition for 'GetString' and no extension method 'GetString' accepting a first argument of type 'ISession' could be found
如何访问这些方法?
推荐答案
GetString
是一个 扩展方法,请确保您有对它的引用.您可能还需要导入它:
GetString
is an extension method in the Microsoft.AspNetCore.Http.Extensions
assembly, make sure you have a reference to that. You may also need to import it:
using Microsoft.AspNetCore.Http;
这篇关于IHttpContextAccessor 会话 GetString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!