HttpCookie cookie = new HttpCookie("ReportCookie");
cookie.Values["a"] = "中华人民共和国";
cookie.Values["b"] = "大家好";
DateTime dtNow = DateTime.Now;
TimeSpan tsMinute = new TimeSpan(0, 1, 0, 0);
cookie.Expires = dtNow + tsMinute;
HttpContext.Current.Response.AppendCookie(cookie);
我在程序中这样写的,运行后出现错误,提示“未将对象引用设置到对象的实例。”
=========================================================
请高人指点一下啊!
cookie.Values["a"] = "中华人民共和国";
cookie.Values["b"] = "大家好";
DateTime dtNow = DateTime.Now;
TimeSpan tsMinute = new TimeSpan(0, 1, 0, 0);
cookie.Expires = dtNow + tsMinute;
HttpContext.Current.Response.AppendCookie(cookie);
我在程序中这样写的,运行后出现错误,提示“未将对象引用设置到对象的实例。”
=========================================================
请高人指点一下啊!
{
StringBuilder sb = new StringBuilder();
// Get cookie from the current request.
HttpCookie cookie = Request.Cookies.Get("DateCookieExample");
// Check if cookie exists in the current request.
if (cookie == null)
{
sb.Append("Cookie was not received from the client. ");
sb.Append("Creating cookie to add to the response. <br/>");
// Create cookie.
cookie = new HttpCookie("DateCookieExample");
// Set value of cookie to current date time.
cookie.Value = DateTime.Now.ToString();
// Set cookie to expire in 10 minutes.
cookie.Expires = DateTime.Now.AddMinutes(10d);
// Insert the cookie in the current HttpResponse.
Response.Cookies.Add(cookie);
}
else
{
sb.Append("Cookie retrieved from client. <br/>");
sb.Append("Cookie Name: " + cookie.Name + "<br/>");
sb.Append("Cookie Value: " + cookie.Value + "<br/>");
sb.Append("Cookie Expiration Date: " +
cookie.Expires.ToString() + "<br/>");
}
Label1.Text = sb.ToString();
}
http://msdn.microsoft.com/zh-cn/library/system.web.httpcookie(VS.80).aspx
可以参考下