主营业务:网站制作 网站优化 小程序制作 石家庄网站制作网站建设有限公司欢迎您!
石家庄闪云网络科技有限公司
客服热线15383239821
如何让自己的网站更有质量?

ASP.NET中Cookies的用法

发布时间:2013/4/23 22:01:13

一, cookies 写入

方法1: 
Response.Cookies["username"].Value="gjy"; 
Response.Cookies["username"].Expires=DateTime.Now.AddDays(1); 

方法2: 
System.Web.HttpCookie newcookie=new HttpCookie("username"); 
newcookie.Value="gjy"; 
newcookie.Expires=DateTime.Now.AddDays(1); 
Response.AppendCookie(newcookie);

 

创建带有子键的cookies: 
System.Web.HttpCookie newcookie=new HttpCookie("user"); 
newcookie.Values["username"]="gjy"; 
newcookie.Values["password"]="111"; 
newcookie.Expires=DateTime.Now.AddDays(1); 
Response.AppendCookie(newcookie);

或者

HttpCookie UserCookie = new HttpCookie("KindCode");
UserCookie["bigKind"] = lstBigKindCode.SelectedValue.Trim();
UserCookie["smallKind"] = lstSmallKindCode.SelectedValue.Trim();
UserCookie["UserName"] = strUserName;
UserCookie["userKind"] = lsbUserSmallKindCode.SelectedValue;
UserCookie.Expires = DateTime.Now.AddDays(1);//这里设置要保存多长时间.
Response.Cookies.Add(UserCookie);

 

二,cookies的读取: 

无子键读取: 
if(Request.Cookies["username"]!=null) 

Response.Write(Server.HtmlEncode(Request.Cookies["username"].Value)); 


有子键读取: 
if(Request.Cookies["user"]!=null) 

Response.Write(Server.HtmlEncode(Request.Cookies["user"]["username"].Value));

}

或者

HttpCookie cookie = Request.Cookies["KindCode"];
if (cookie != null)
{
string bigKind = cookie.Values["bigKind"];
string userName = cookie.Values["UserName"];

}

三,cookies的清除

HttpCookie cookie = Request.Cookies["KindCode"];
if (cookie != null)
{
cookie.Expires = DateTime.Now.AddDays(-2);
Response.Cookies.Set(cookie);

}

上一篇: C#读取设置Cookie
下一篇: Cookie应用小总结

相关新闻推荐

在线客服 : 服务热线:15383239821 电子邮箱: 27535611@qq.com

公司地址:石家庄市新华区九中街江西大厦4062

备案号:冀ICP备2022000585号-4