IHttpContextAccessor Session GetString

安稳与你 提交于 2021-02-07 11:38:12

问题


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]

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.

I have seen people in other questions using HttpContext.Session.GetString() and HttpContext.Session.SetString():

Access HttpContext.Current

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' does not contain a definition for 'GetString' and no extension method 'GetString' accepting a first argument of type 'ISession' could be found

How to I get access to these methods?


回答1:


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;


来源:https://stackoverflow.com/questions/44850909/ihttpcontextaccessor-session-getstring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!