Asp.net Core 5.0 在使用时似乎没有 httpContext.Session.GetString 作为方法

si2*_*030 4 c# asp.net-core

我正在迁移到 ASP.NET CORE 5.0 并设置了中间件,但是在设置我项目的这一部分时,我遇到httpContext.Session.GetString了一个错误。这可以使用,但似乎他们已经删除了 .GetString 和 .SetString。

这是我的中间件代码。

public class ConfigureSessionMiddleware
    {
        private readonly RequestDelegate _next;

        public ConfigureSessionMiddleware(RequestDelegate next)
        {
            _next = next;
        }

        public async Task InvokeAsync(HttpContext httpContext, IUserSession userSession, ISessionServices sessionServices)
        {
            if (httpContext == null)
            {
                throw new ArgumentNullException(nameof(httpContext));
            }

            if (userSession == null)
            {
                throw new ArgumentNullException(nameof(userSession));
            }

            if (sessionServices == null)
            {
                throw new ArgumentNullException(nameof(sessionServices));
            }

            if (httpContext.User.Identities.Any(id => id.IsAuthenticated))
            {
                if (httpContext.Session.GetString("connectionString") == null) // Session needs to be set..
                {
                    userSession.UserId = httpContext.User.Claims.FirstOrDefault(x => x.Type == "userId")?.Value;
                    userSession.ConnectionString = sessionServices.ConnectionStringFromUserId(userSession.UserId);
                    httpContext.Session.SetString("userId", userSession.UserId);
                    httpContext.Session.SetString("connectionString", userSession.ConnectionString);
                }
                else  //  Session set so all we need to is to build userSession for data access..
                {
                    userSession.UserId = httpContext.Session.GetString("userId");
                    userSession.ConnectionString = httpContext.Session.GetString("connectionString");
                }
            }

            // Call the next delegate/middleware in the pipeline
            await _next.Invoke(httpContext).ConfigureAwait(false);
        }
    }
Run Code Online (Sandbox Code Playgroud)

它在以下代码段出错:

if (httpContext.Session.GetString("connectionString") == null) // Session needs to be set..
Run Code Online (Sandbox Code Playgroud)

我得到的错误是:

“ISession”不包含“SetString”的定义,并且找不到接受“ISession”类型的第一个参数的可访问扩展方法“SetString”(您是否缺少 using 指令或程序集引用?)

我用智能感知注意到GetStringSetString显示但背后有问号..

GetString 后面有问号

所以我的问题是,如果我不能GetString在这个例子中使用访问(或创建)我的 var 连接字符串,我如何检查/访问/创建一个变量,例如 httpContext.session 中的“connectionstring”,因为这些方法已被弃用?

Ath*_*ras 5

net.core 5.0 Preview 中尚不存在这些扩展。如果您转到文档,它会重定向到 3.1

请求的页面不可用于 ASP.NET Core 5.0 预览版。您已被重定向到此页面可用的最新产品版本。

为什么不自己添加它们?它们只是网络核心扩展@github

        public static void SetString(this ISession session, string key, string value)
        {
            session.Set(key, Encoding.UTF8.GetBytes(value));
        }

        public static string GetString(this ISession session, string key)
        {
            var data = session.Get(key);
            if (data == null)
            {
                return null;
            }
            return Encoding.UTF8.GetString(data);
        }
Run Code Online (Sandbox Code Playgroud)

  • 现在您可以安装“Microsoft.AspNetCore.Http.Extensions” NuGet 包 /sf/answers/3139578571/ (2认同)