繁体   English   中英

我如何将某些内容保存在Cookie C#asp.net中

[英]how do i save something in a cookie c# asp.net

我只想将类的实例保存在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;

请帮忙。 提前致谢

Value属性定义为字符串类型-在两个示例中,您似乎都在尝试给它提供一个类khurram

这样的事情可能更适合您:

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

// later ...

khurram k1= new khurram(); 
HttpCookie tcook = new HttpCookie("test");
tcook.Value = k1.str1;
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.");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM