码迷,mamicode.com
首页 > Windows程序 > 详细

C# WebHelper-CookieHelper,CacheHelper,SessionHelper

时间:2017-12-30 19:10:46      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:工具   datetime   cti   读取   tee   stat   setcookie   取数   web   

常用web操作工具类,记录一下,本文记录的工具类,都要求引用 System.Web

1、CookieHelper

技术分享图片
 1 /// <summary>
 2     /// Cookie工具类
 3     /// </summary>
 4     public class CookieHelper
 5     {
 6         /// <summary>
 7         /// 清除指定Cookie
 8         /// </summary>
 9         /// <param name="cookiename">cookiename</param>
10         public static void ClearCookie(string cookiename)
11         {
12             HttpCookie cookie = HttpContext.Current.Request.Cookies[cookiename];
13             if (cookie != null)
14             {
15                 cookie.Expires = DateTime.Now.AddYears(-3);
16                 HttpContext.Current.Response.Cookies.Add(cookie);
17             }
18         }
19         /// <summary>
20         /// 获取指定Cookie值
21         /// </summary>
22         /// <param name="cookiename">cookiename</param>
23         /// <returns></returns>
24         public static string GetCookieValue(string cookiename)
25         {
26             HttpCookie cookie = HttpContext.Current.Request.Cookies[cookiename];
27             string str = string.Empty;
28             if (cookie != null)
29             {
30                 str = cookie.Value;
31             }
32             return str;
33         }
34         /// <summary>
35         /// 添加一个Cookie(24小时过期)
36         /// </summary>
37         /// <param name="cookiename"></param>
38         /// <param name="cookievalue"></param>
39         public static void SetCookie(string cookiename, string cookievalue)
40         {
41             SetCookie(cookiename, cookievalue, DateTime.Now.AddDays(1.0));
42         }
43         /// <summary>
44         /// 添加一个Cookie
45         /// </summary>
46         /// <param name="cookiename">cookie名</param>
47         /// <param name="cookievalue">cookie值</param>
48         /// <param name="expires">过期时间 DateTime</param>
49         public static void SetCookie(string cookiename, string cookievalue, DateTime expires)
50         {
51             HttpCookie cookie = new HttpCookie(cookiename)
52             {
53                 Value = cookievalue,
54                 Expires = expires
55             };
56             HttpContext.Current.Response.Cookies.Add(cookie);
57         }
58     }
CookieHelper

 

 2、SessionHelper

技术分享图片
  1 /// <summary>
  2     /// Session 操作类
  3     /// </summary>
  4     public class SessionHelper
  5     {
  6         /// <summary>
  7         /// 根据session名获取session对象
  8         /// </summary>
  9         /// <param name="name">session 名</param>
 10         /// <returns></returns>
 11         public static object GetSession(string name)
 12         {
 13             return HttpContext.Current.Session[name];
 14         }
 15         /// <summary>
 16         /// 设置session
 17         /// </summary>
 18         /// <param name="name">session 名</param>
 19         /// <param name="val">session 值</param>
 20         public static void SetSession(string name, object val)
 21         {
 22             HttpContext.Current.Session.Remove(name);
 23             HttpContext.Current.Session.Add(name, val);
 24         }
 25         /// <summary>
 26         /// 添加Session,调动有效期为20分钟
 27         /// </summary>
 28         /// <param name="strSessionName">Session对象名称</param>
 29         /// <param name="strValue">Session值</param>
 30         public static void Add(string strSessionName, string strValue)
 31         {
 32             HttpContext.Current.Session[strSessionName] = strValue;
 33             HttpContext.Current.Session.Timeout = 20;
 34         }
 35 
 36         /// <summary>
 37         /// 添加Session,调动有效期为20分钟
 38         /// </summary>
 39         /// <param name="strSessionName">Session对象名称</param>
 40         /// <param name="strValues">Session值数组</param>
 41         public static void Adds(string strSessionName, string[] strValues)
 42         {
 43             HttpContext.Current.Session[strSessionName] = strValues;
 44             HttpContext.Current.Session.Timeout = 20;
 45         }
 46 
 47         /// <summary>
 48         /// 添加Session
 49         /// </summary>
 50         /// <param name="strSessionName">Session对象名称</param>
 51         /// <param name="strValue">Session值</param>
 52         /// <param name="iExpires">调动有效期(分钟)</param>
 53         public static void Add(string strSessionName, string strValue, int iExpires)
 54         {
 55             HttpContext.Current.Session[strSessionName] = strValue;
 56             HttpContext.Current.Session.Timeout = iExpires;
 57         }
 58 
 59         /// <summary>
 60         /// 添加Session
 61         /// </summary>
 62         /// <param name="strSessionName">Session对象名称</param>
 63         /// <param name="strValues">Session值数组</param>
 64         /// <param name="iExpires">调动有效期(分钟)</param>
 65         public static void Adds(string strSessionName, string[] strValues, int iExpires)
 66         {
 67             HttpContext.Current.Session[strSessionName] = strValues;
 68             HttpContext.Current.Session.Timeout = iExpires;
 69         }
 70 
 71         /// <summary>
 72         /// 读取某个Session对象值
 73         /// </summary>
 74         /// <param name="strSessionName">Session对象名称</param>
 75         /// <returns>Session对象值</returns>
 76         public static object Get(string strSessionName)
 77         {
 78             if (HttpContext.Current.Session[strSessionName] == null)
 79             {
 80                 return null;
 81             }
 82             else
 83             {
 84                 return HttpContext.Current.Session[strSessionName];
 85             }
 86         }
 87 
 88         /// <summary>
 89         /// 读取某个Session对象值数组
 90         /// </summary>
 91         /// <param name="strSessionName">Session对象名称</param>
 92         /// <returns>Session对象值数组</returns>
 93         public static string[] Gets(string strSessionName)
 94         {
 95             if (HttpContext.Current.Session[strSessionName] == null)
 96             {
 97                 return null;
 98             }
 99             else
100             {
101                 return (string[])HttpContext.Current.Session[strSessionName];
102             }
103         }
104 
105         /// <summary>
106         /// 删除某个Session对象
107         /// </summary>
108         /// <param name="strSessionName">Session对象名称</param>
109         public static void Del(string strSessionName)
110         {
111             HttpContext.Current.Session[strSessionName] = null;
112         }
113     }
SessionHelper

 

3、CacheHelper

技术分享图片
 1 /// <summary>
 2     /// 缓存辅助类
 3     /// </summary>
 4     public class CacheHelper
 5     {
 6         /// <summary>
 7         /// 获取数据缓存
 8         /// </summary>
 9         /// <param name="CacheKey"></param>
10         public static object GetCache(string CacheKey)
11         {
12             System.Web.Caching.Cache objCache = HttpRuntime.Cache;
13             return objCache[CacheKey];
14         }
15 
16         /// <summary>
17         /// 设置数据缓存
18         /// </summary>
19         public static void SetCache(string CacheKey, object objObject)
20         {
21             System.Web.Caching.Cache objCache = HttpRuntime.Cache;
22             objCache.Insert(CacheKey, objObject);
23         }
24 
25         /// <summary>
26         /// 设置数据缓存
27         /// </summary>
28         public static void SetCache(string CacheKey, object objObject, TimeSpan Timeout)
29         {
30             System.Web.Caching.Cache objCache = HttpRuntime.Cache;
31             objCache.Insert(CacheKey, objObject, null, DateTime.MaxValue, Timeout, System.Web.Caching.CacheItemPriority.NotRemovable, null);
32         }
33 
34         /// <summary>
35         /// 设置数据缓存
36         /// </summary>
37         public static void SetCache(string CacheKey, object objObject, DateTime absoluteExpiration, TimeSpan slidingExpiration)
38         {
39             System.Web.Caching.Cache objCache = HttpRuntime.Cache;
40             objCache.Insert(CacheKey, objObject, null, absoluteExpiration, slidingExpiration);
41         }
42 
43         /// <summary>
44         /// 移除指定数据缓存
45         /// </summary>
46         public static void RemoveAllCache(string CacheKey)
47         {
48             System.Web.Caching.Cache _cache = HttpRuntime.Cache;
49             _cache.Remove(CacheKey);
50         }
51 
52         /// <summary>
53         /// 移除全部缓存
54         /// </summary>
55         public static void RemoveAllCache()
56         {
57             System.Web.Caching.Cache _cache = HttpRuntime.Cache;
58             IDictionaryEnumerator CacheEnum = _cache.GetEnumerator();
59             while (CacheEnum.MoveNext())
60             {
61                 _cache.Remove(CacheEnum.Key.ToString());
62             }
63         }
64     }
CacheHelper

 

C# WebHelper-CookieHelper,CacheHelper,SessionHelper

标签:工具   datetime   cti   读取   tee   stat   setcookie   取数   web   

原文地址:https://www.cnblogs.com/PiaoMiaoGongZi/p/8150617.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!