我用的vs2005,写了一个基类PageBae
想使用Cookie进行取用户信息
写了个方法如下public int CookiesUserID
    {
  
      get
        {
           
            if (HttpContext.Current.Request.Cookies["UserID"] != null)
            {
                                return int.Parse(HttpContext.Current.Request.Cookies["UserID"].ToString().Trim());
            }
            else
            {
                return -1;
            }
        }        set
         {
      
          HttpContext.Current.Request.Cookies["UserID"] = value;
            
        }
    }在页面中继承了这个类,使用就是出错,如果换用Session就没有任何问题。
如果将set里去掉可运行但Cookie取不到数据

解决方案 »

  1.   

    cookie的设置不是这样的吧?Dim testCookie As New HttpCookie("LastVisited")testCookie.Value = DateTime.Now.ToStringtestCookie.Expires = DateTime.Now.AddDays(7)Response.Cookies.Add(testCookie) 
      

  2.   

    try
    :public int CookiesUserID
        {
      
          get
            {
               
                if (HttpContext.Current.Request.Cookies["myCook"] != null)
                {
                                    return int.Parse(HttpContext.Current.Request.Cookies["myCook"]["UserID"].ToString().Trim());
                }
                else
                {
                    return -1;
                }
            }        set
             {
                 if (HttpContext.Current.Request.Cookies["myCook"] != null)
                 {
                        HttpContext.Current.Request.Cookies["myCook"]["UserID"] = value;
                  }else
                            {
         
                 HttpContext.CuRENT.Response.Cookies.Add(new HttpCookie["myCook"]);
               HttpContext.CuRENT.Response.Cookies["myCook"]["UserID"] = value;}
                   
           
                
            }
        }-----难免有错,错了莫怪.
      

  3.   

    谢谢lazyfish(呆呆虫) ,cpp2017(幕白兄)解答运行出错
    Compiler Error Message: CS0029: Cannot implicitly convert type 'int' to 'string'Source Error: Line 43:             if (HttpContext.Current.Request.Cookies["myCook"] != null)
    Line 44:             {
    Line 45:                 HttpContext.Current.Request.Cookies["myCook"]["UserID"] = value;
    Line 46:             }45行出错
     
      

  4.   

    对了要转换一下,类型是int的.-----难免有错,错了莫怪.
      

  5.   

    我这样转换了int.Parse
    出错提示如下 CS0131: The left-hand side of an assignment must be a variable, property or indexerSource Error: Line 43:             if (HttpContext.Current.Request.Cookies["myCook"] != null)
    Line 44:             {
    Line 45:                 int.Parse(HttpContext.Current.Request.Cookies["myCook"]["UserID"].ToString().Trim()) = value;
    Line 46:             }
    Line 47:             else
     
      

  6.   

    public int CookiesUserID
    {
      
    get
    {
               
    if (HttpContext.Current.Request.Cookies["myCook"] != null)
    {
                     return int.Parse(HttpContext.Current.Request.Cookies["myCook"]["UserID"].ToString().Trim());
    }
    else
    {
    return -1;
    }
    } set
    {
    if (HttpContext.Current.Request.Cookies["myCook"] != null)
    {
    HttpContext.Current.Request.Cookies["myCook"]["UserID"] = value.ToString();
    }
    else
    {
         
    HttpContext.Current.Response.Cookies.Add(new HttpCookie("myCook"));
    HttpContext.Current.Response.Cookies["myCook"]["UserID"] = value.ToString(); }
                   
           
                
    }
    }-----难免有错,错了莫怪.
      

  7.   

    感谢cpp2017(幕白兄)热心帮忙,我已经改过来了,可以了
    再请问一下如何设置时间呢?比如说保存一个星期一个月
      

  8.   

    else
    {
         
    HttpContext.Current.Response.Cookies.Add(new HttpCookie("myCook"));
    HttpContext.Current.Response.Cookies["myCook"]["UserID"] = value.ToString(); }将HttpContext.Current.Response.Cookies.Add(new HttpCookie("myCook"));去掉就可以了。
      

  9.   

    set  的 else
    HttpCookie cook = new HttpCookie("myCook");
    cook.Expires  = DateTime.Now.AddMonths(1); HttpContext.Current.Response.Cookies.Add(cook);
    HttpContext.Current.Response.Cookies["myCook"]["UserID"] = value.ToString();-----难免有错,错了莫怪.