HttpContext中的会话存储IQueryable

时间:2019-03-20 08:41:07

标签: c# asp.net-core .net-core asp.net-core-mvc .net-core-2.0

我正在将项目从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是可查询的

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

HttpContext.Session.SetString(_currentOwnersResult, allResult);

错误代码:

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

1 个答案:

答案 0 :(得分:4)

好的,作为一个有关如何在.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);               
  });
}

要将列表中的复杂对象添加到Session中(请注意,这只是一个示例,并不特定于您的情况,因为我不知道什么是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();