日期:2014-05-17  浏览次数:20455 次

cs类 添加cookie
在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为何不行

------解决方案--------------------

http://www.cnblogs.com/zhongweiv/archive/2011/11/08/Cookies.html
------解决方案--------------------
HttpContext.Current.Request.Cookies[x]
得到
------解决方案--------------------
HttpContext.Current.Response.Cookies.Set(New HttpCookie("baidu"))

如果 cookie》baidu 已存在 自动覆盖 否则新增
------解决方案--------------------
c# 是
C# code
HttpContext.Current.Response.Cookies.Set(new HttpCookie("baidu"));

------解决方案--------------------
HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];

Response.Cookies换了页面就没有了

不是早告诉你了吗
------解决方案--------------------
Request.Cookies 和 Response.Cookies 的区别
------解决方案--------------------
C# code

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);
        }
    }
}

------解决方案--------------------
HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];