【问题标题】:Asp.net Core 5.0 does not appear to have httpContext.Session.GetString as a method when it use toAsp.net Core 5.0 在使用时似乎没有 httpContext.Session.GetString 作为方法
【发布时间】:2020-09-21 16:35:44
【问题描述】:

我正在迁移到 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);
        }
    }

以下代码出错:

if (httpContext.Session.GetString("connectionString") == null) // Session needs to be set..

我得到的错误是:

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

我注意到 GetStringSetString 显示但后面有问号..

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

【问题讨论】:

  • 智能感知中的那些问号意味着它采用了该方法,因为您多次调用它,但它不存在,无论是因为您错过并导入还是因为它根本不存在不存在。当您开始使用变量而没有实际声明它时,也会发生同样的事情
  • 我猜您正在从直接引用扩展程序集的版本迁移。那就是汇编现在只是框架的一部分。
  • 感谢您的指导,但是我确实查找了“Microsoft.AspNetCore.Http.Extensions”,并且它仅在 2.2.0 版本中 - 我在 5.0 中工作。另外,我有一个想法,它只是拿起了我所做的,而不是有一个实际的方法。所以鉴于我在 5.0 rc 中工作(我正在迁移是的),有没有办法在这个特定版本中做到这一点。

标签: c# asp.net-core


【解决方案1】:

net.core 5.0 预览版中尚不存在这些扩展。如果您转到the documentation,它会重定向到 3.1

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

为什么不自己添加呢?他们只是net core extensions@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);
        }

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-03-27
  • 1970-01-01
  • 1970-01-01
  • 2021-09-23
  • 1970-01-01
  • 2019-09-07
  • 1970-01-01
相关资源
最近更新 更多