我如何在cookie#asp.net中保存一些东西

时间:2014-04-01 11:17:27

标签: c# asp.net cookies

所有我想在cookie中保存一个类的实例只是为了检查一些东西。 这是我的代码

    class khurram { 
khurram k1= new khurram(); 
HttpCookie tcook = new HttpCookie("test");
            tcook.Value = k1;
}

但是' tcook'不在场。我做错了什么我不明白。

我也试过

[serializable]
class khurram { 
public string str1{get;set;};
}
khurram k1= new khurram(); 
HttpCookie tcook = new HttpCookie("test");
tcook.Value = k1;

请帮忙。 提前谢谢

2 个答案:

答案 0 :(得分:2)

Value property被定义为字符串类型 - 在您的两个示例中,您似乎都试图为其提供类khurram

这样的一些人可能会对你更好:

class khurram { 
    public string str1{get;set;};
}

// later ...

khurram k1= new khurram(); 
HttpCookie tcook = new HttpCookie("test");
tcook.Value = k1.str1;

答案 1 :(得分:0)

HttpCookie myCookie = new HttpCookie("MyTestCookie");
DateTime now = DateTime.Now;

// Set the cookie value.
myCookie.Value = now.ToString();
// Set the cookie expiration date.
myCookie.Expires = now.AddMinutes(1);

// Add the cookie.
Response.Cookies.Add(myCookie);

Response.Write("<p> The cookie has been written.");