HttpContext 中的会话存储 IQueryable

Session Store IQueryable in HttpContext

我正在将项目从 Net MVC 迁移到 MVC Core 2。

如何在会话中设置 IQueryable? 在 Net MVC 中是这样的,

    public ActionResult CurrentOwners_Read([DataSourceRequest]DataSourceRequest request, int propertyID)
    {
        if (propertyID == 0)
        {
            throw new ArgumentNullException("propertyID");
        }

        IQueryable<PropertyOwnerRoleViewModel> allResult = (IQueryable<PropertyOwnerRoleViewModel>)HttpContext.Session.GetString(_currentOwnersResult).AsQueryable();

        if (allResult == null)
        {
            PropertyOwnerManager propertyOwnerManager = new PropertyOwnerManager();
            allResult = propertyOwnerManager.GetPropertyOwnershipSummary(propertyID).AsQueryable();
            Session.Add(_currentOwnersResult, allResult);  
        }

上面最后一行给出错误:

The name 'Session' does not exist in the current context

_currentOwnersResult 是字符串 AllResult 是 IQueryable

尝试在 MVC Core 中进行转换时,以下内容也不起作用

HttpContext.Session.SetString(_currentOwnersResult, allResult);

错误代码:

cannot convert from 'System.Linq.IQueryable<HPE.Kruta.Model.PropertyOwnerRoleViewModel>' to 'string'    

好的,作为一个关于如何在 .NET Core 的 Session 中设置复杂对象的基本示例:

首先设置您的会话:

在您的 Startup.cs 中,在 Configure 方法下,添加以下行:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseSession();
}

并在 ConfigureServices 方法下,添加以下行:

public void ConfigureServices(IServiceCollection services)
{
  //Added for session state
  services.AddDistributedMemoryCache();

  services.AddSession(options =>
  {
  options.IdleTimeout = TimeSpan.FromMinutes(10);               
  });
}

将列表中的复杂对象添加到会话中(请注意,这只是一个示例,并不特定于您的情况,因为我不知道 PropertyOwnerRoleViewModel 定义是什么):

型号:

public class EmployeeDetails
{
    public string EmployeeId { get; set; }
    public string DesignationId { get; set; }
}

public class EmployeeDetailsDisplay
{
    public EmployeeDetailsDisplay()
    {
        details = new List<EmployeeDetails>();
    }
    public List<EmployeeDetails> details { get; set; }
}

然后创建一个 SessionExtension 助手来设置和检索您的复杂对象 JSON:

public static class SessionExtensions
        {
            public static void SetObjectAsJson(this ISession session, string key, object value)
            {
                session.SetString(key, JsonConvert.SerializeObject(value));
            }

            public static T GetObjectFromJson<T>(this ISession session, string key)
            {
                var value = session.GetString(key);

                return value == null ? default(T) : JsonConvert.DeserializeObject<T>(value);
            }
        }

正在将此复杂对象添加到您的会话中:

 //Create complex object
 var employee = new EmployeeDetailsDisplay();

 employee.details.Add(new EmployeeDetails
 {
 EmployeeId = "1",
 DesignationId = "2"
 });

 employee.details.Add(new EmployeeDetails
 {
 EmployeeId = "3",
 DesignationId = "4"
 });
//Add to your session
HttpContext.Session.SetObjectAsJson("EmployeeDetails", employee);

最后从会话中检索复杂对象:

var employeeDetails = HttpContext.Session.GetObjectFromJson<EmployeeDetailsDisplay>("EmployeeDetails");

//Get list of all employee id's
List<int> employeeID = employeeDetails.details.Select(x => Convert.ToInt32(x.EmployeeId)).ToList();
//Get list of all designation id's
List<int> designationID= employeeDetails.details.Select(x=> Convert.ToInt32(x.DesignationId)).ToList();