Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#-ASP.NET MVC-将对象列表转换为字符串列表,以便在会话中使用_C#_Asp.net Mvc_Session - Fatal编程技术网

C#-ASP.NET MVC-将对象列表转换为字符串列表,以便在会话中使用

C#-ASP.NET MVC-将对象列表转换为字符串列表,以便在会话中使用,c#,asp.net-mvc,session,C#,Asp.net Mvc,Session,我有一份打印的清单: IEnumerable<Resource> ResourceAuthorizedForLoggedUser= resourceAuthorizer.FindAll(); 我需要在会议中说明: System.Web.HttpContext.Current.Session["Authorized_List"] = authorizations; 能够在基本控制器中访问此数据 public class BaseController : Controller {

我有一份打印的清单:

IEnumerable<Resource> ResourceAuthorizedForLoggedUser= resourceAuthorizer.FindAll();
我需要在会议中说明:

System.Web.HttpContext.Current.Session["Authorized_List"] = authorizations;
能够在基本控制器中访问此数据

public class BaseController : Controller
{
   public static List<string> authorizations { get { return Getauthorizations(); } }

   private static List<string> Getauthorizations()
   {
      List<string> authorizationsList = (List<string>)System.Web.HttpContext.Current.Session["Authorized_List"];
      return authorizationsList != null && authorizationsList.Any() ? authorizationsList : new List<string>();
   }
}
公共类BaseController:控制器
{
公共静态列表授权{get{return Getauthorizations();}}
私有静态列表Getauthorizations()
{
列表授权列表=(列表)System.Web.HttpContext.Current.Session[“Authorized_List”];
return authorizationsList!=null&&authorizationsList.Any()?authorizationsList:new List();
}
}
最后,我在
Getauthorizations
方法中得到以下错误:

无法将“System.String”类型的对象转换为“System.Collections.Generic.List`1[System.String]”类型


您知道如何转换该列表吗?

我将按如下方式编写您的代码:

  • 首先,您将获得资源类型的集合:
  • 从会话中读回:
  • 公共类BaseController:控制器
    {
    公共静态列表授权{get{return Getauthorizations();}}
    私有静态列表Getauthorizations()
    {
    列表授权列表=(列表)System.Web.HttpContext.Current.Session[“Authorized_List”];
    return authorizationsList!=null&&authorizationsList.Any()?authorizationsList:new List();
    }
    }`在这里输入代码`
    

    设置会话变量时,
    ToList()
    而不是
    ToString()
    ResourceAuthorizedForLoggedUser。选择(x=>x.Id)
    只会获得一个用户,为什么要将其列为列表?在
    System.Web.HttpContext.Current.Session[“Authorized_List”]=authorizations上设置断点
    什么类型的
    authorizations
    ,它不是你想象的那样。@choěxěŕ我本以为
    ResourceAuthorizator.FindAll()
    可以返回多个对象。@JohnathanBarclay是的,但看看
    ResourceAuthorizatedForLoggeDuser.Select(x=>x.Id)。ToString()
    。。。这不会产生一个集合:)@đěxěŕ是的,因此我最初的评论;它应该是
    。选择(x=>x.Id)。ToList()
    public class BaseController : Controller
    {
       public static List<string> authorizations { get { return Getauthorizations(); } }
    
       private static List<string> Getauthorizations()
       {
          List<string> authorizationsList = (List<string>)System.Web.HttpContext.Current.Session["Authorized_List"];
          return authorizationsList != null && authorizationsList.Any() ? authorizationsList : new List<string>();
       }
    }
    
     IEnumerable<Resource> ResourceAuthorizedForLoggedUser=
     resourceAuthorizer.FindAll();
    
     List<string> authorizations=
     ResourceAuthorizedForLoggedUser.Select(x=> x.Id).ToList();
    
     System.Web.HttpContext.Current.Session["Authorized_List"] =
     authorizations;
    
    public class BaseController : Controller
         {
            public static List<string> authorizations { get { return Getauthorizations(); } }
    
    
       private static List<string> Getauthorizations()
       {
          List<string> authorizationsList = (List<string>)System.Web.HttpContext.Current.Session["Authorized_List"];
          return authorizationsList != null && authorizationsList.Any() ? authorizationsList : new List<string>();
       }
    }`enter code here`