文章出處

常用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

 


文章列表




Avast logo

Avast 防毒軟體已檢查此封電子郵件的病毒。
www.avast.com


arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()