[英]Asp.net Core 5.0 does not appear to have httpContext.Session.GetString as a method when it use to
我正在遷移到 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”的定義,並且找不到接受“ISession”類型的第一個參數的可訪問擴展方法“SetString”(您是否缺少 using 指令或程序集引用?)
我用智能感知注意到GetString
和SetString
顯示但背后有問號..
所以我的問題是,如果我不能在這個例子中使用GetString
訪問(或創建)我的 var 連接字符串,我如何在 httpContext.session 中檢查/訪問/創建一個變量,例如“connectionstring”,因為這些方法已被棄用?
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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.