在cs类中
获取cookie
若为null则添加
否则修改原cookie值引用http://blog.sina.com.cn/s/blog_5ff9cc430100i17c.html
CookiesHelper操作类
在HttpResponse response = HttpContext.Current.Response;
总是null
该类 后来继承了 System.Web.UI.Page
一样不行
---------------------------------------------------------
我的想法是程序变动塞值
页面js 秒判 值是否变动
否则刷新该页面
现在测试在aspx 的类文件中 添加没问题
只是在单纯的cs类文件中 的操作 报HttpContext.Current.Response=null
页面js 的秒判 cookie值 也能获得求达人释疑
单纯的cs类文件 操作cookie为何不行
获取cookie
若为null则添加
否则修改原cookie值引用http://blog.sina.com.cn/s/blog_5ff9cc430100i17c.html
CookiesHelper操作类
在HttpResponse response = HttpContext.Current.Response;
总是null
该类 后来继承了 System.Web.UI.Page
一样不行
---------------------------------------------------------
我的想法是程序变动塞值
页面js 秒判 值是否变动
否则刷新该页面
现在测试在aspx 的类文件中 添加没问题
只是在单纯的cs类文件中 的操作 报HttpContext.Current.Response=null
页面js 的秒判 cookie值 也能获得求达人释疑
单纯的cs类文件 操作cookie为何不行
http://www.cnblogs.com/zhongweiv/archive/2011/11/08/Cookies.html
得到
HttpContext.Current.Response.Cookies.Set(new HttpCookie("baidu"));
if (cookie != null)
{
if (!string.IsNullOrEmpty(key) && cookie.HasKeys)
cookie.Values.Set(key, value);
else
if (!string.IsNullOrEmpty(value))
cookie.Value = value;
if (expires != null)
cookie.Expires = expires.Value;
HttpContext.Current.Response.SetCookie(cookie);
}
现在改成这样
HttpCookie cookie = HttpContext.Current.Response.Cookies[cookieName];
就报错 跳异常了 不行啊
if (cookie != null)
{
if (!string.IsNullOrEmpty(key) && cookie.HasKeys)
cookie.Values.Set(key, value);
else
if (!string.IsNullOrEmpty(value))
cookie.Value = value;
if (expires != null)
cookie.Expires = expires.Value;
HttpContext.Current.Response.SetCookie(cookie);
}
现在改成这样
HttpCookie cookie = HttpContext.Current.Response.Cookies[cookieName];
就报错 跳异常了 不行啊
是cs类 操作cookie时
HttpCookie cookie = HttpContext.Current.Response.Cookies[cookieName];
HttpResponse response = HttpContext.Current.Response;
就开始跳异常
继承System.Web.UI.Page 也没用
using System;
using System.Web;namespace DotNet.Utilities
{
public class CookieHelper
{
/// <summary>
/// 清除指定Cookie
/// </summary>
/// <param name="cookiename">cookiename</param>
public static void ClearCookie(string cookiename)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[cookiename];
if (cookie != null)
{
cookie.Expires = DateTime.Now.AddYears(-3);
HttpContext.Current.Response.Cookies.Add(cookie);
}
}
/// <summary>
/// 获取指定Cookie值
/// </summary>
/// <param name="cookiename">cookiename</param>
/// <returns></returns>
public static string GetCookieValue(string cookiename)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[cookiename];
string str = string.Empty;
if (cookie != null)
{
str = cookie.Value;
}
return str;
}
/// <summary>
/// 添加一个Cookie(24小时过期)
/// </summary>
/// <param name="cookiename"></param>
/// <param name="cookievalue"></param>
public static void SetCookie(string cookiename, string cookievalue)
{
SetCookie(cookiename, cookievalue, DateTime.Now.AddDays(1.0));
}
/// <summary>
/// 添加一个Cookie
/// </summary>
/// <param name="cookiename">cookie名</param>
/// <param name="cookievalue">cookie值</param>
/// <param name="expires">过期时间 DateTime</param>
public static void SetCookie(string cookiename, string cookievalue, DateTime expires)
{
HttpCookie cookie = new HttpCookie(cookiename)
{
Value = cookievalue,
Expires = expires
};
HttpContext.Current.Response.Cookies.Add(cookie);
}
}
}
想要把值塞进去 可每次都是添加的时候 报
未将对象引用设置到对象的实例
HttpContext.Current.Response跟踪监视值为null
怎么回事啊 进不去 HttpCookie cookie 值都塞进去了
if (cookie == null)
{
cookie = new HttpCookie("cookieName");
}
cookie.Value = value;
cookie.Expires = DateTime.Now.AddDays(100);
cookie.Path = "/";
HttpContext.Current.Response.Cookies.Add(cookie);
if (cookie == null)
{
cookie = new HttpCookie("cookieName");
}
cookie.Value = value;
cookie.Expires = DateTime.Now.AddDays(100);
cookie.Path = "/";
HttpContext.Current.Response.Cookies.Add(cookie);