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
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屋!