会话存储在HttpContext中可以使用IQueryable

问题描述 投票:1回答:1

我正在将项目从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'    
c# asp.net-core .net-core asp.net-core-mvc .net-core-2.0
1个回答
2
投票

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

首先设置你的会话:

在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);

最后从Session中检索复杂对象:

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();
© www.soinside.com 2019 - 2024. All rights reserved.