首页 > 代码库 > WebHelper
WebHelper
SessionHelper
-------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
namespace Core.WebHelper
{
/// <summary>
/// Session 操作类
/// 1、GetSession(string name)根据session名获取session对象
/// 2、SetSession(string name, object val)设置session
/// </summary>
public class SessionHelper
{
/// <summary>
/// 根据session名获取session对象
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static object GetSession(string name)
{
return HttpContext.Current.Session[name];
}
/// <summary>
/// 设置session
/// </summary>
/// <param name="name">session 名</param>
/// <param name="val">session 值</param>
public static void SetSession(string name, object val)
{
HttpContext.Current.Session.Remove(name);
HttpContext.Current.Session.Add(name, val);
}
/// <summary>
/// 添加Session,调动有效期为20分钟
/// </summary>
/// <param name="strSessionName">Session对象名称</param>
/// <param name="strValue">Session值</param>
public static void Add(string strSessionName, string strValue)
{
HttpContext.Current.Session[strSessionName] = strValue;
HttpContext.Current.Session.Timeout = 20;
}
/// <summary>
/// 添加Session,调动有效期为20分钟
/// </summary>
/// <param name="strSessionName">Session对象名称</param>
/// <param name="strValues">Session值数组</param>
public static void Adds(string strSessionName, string[] strValues)
{
HttpContext.Current.Session[strSessionName] = strValues;
HttpContext.Current.Session.Timeout = 20;
}
/// <summary>
/// 添加Session
/// </summary>
/// <param name="strSessionName">Session对象名称</param>
/// <param name="strValue">Session值</param>
/// <param name="iExpires">调动有效期(分钟)</param>
public static void Add(string strSessionName, string strValue, int iExpires)
{
HttpContext.Current.Session[strSessionName] = strValue;
HttpContext.Current.Session.Timeout = iExpires;
}
/// <summary>
/// 添加Session
/// </summary>
/// <param name="strSessionName">Session对象名称</param>
/// <param name="strValues">Session值数组</param>
/// <param name="iExpires">调动有效期(分钟)</param>
public static void Adds(string strSessionName, string[] strValues, int iExpires)
{
HttpContext.Current.Session[strSessionName] = strValues;
HttpContext.Current.Session.Timeout = iExpires;
}
/// <summary>
/// 读取某个Session对象值
/// </summary>
/// <param name="strSessionName">Session对象名称</param>
/// <returns>Session对象值</returns>
public static object Get(string strSessionName)
{
if (HttpContext.Current.Session[strSessionName] == null)
{
return null;
}
else
{
return HttpContext.Current.Session[strSessionName];
}
}
/// <summary>
/// 读取某个Session对象值数组
/// </summary>
/// <param name="strSessionName">Session对象名称</param>
/// <returns>Session对象值数组</returns>
public static string[] Gets(string strSessionName)
{
if (HttpContext.Current.Session[strSessionName] == null)
{
return null;
}
else
{
return (string[])HttpContext.Current.Session[strSessionName];
}
}
/// <summary>
/// 删除某个Session对象
/// </summary>
/// <param name="strSessionName">Session对象名称</param>
public static void Del(string strSessionName)
{
HttpContext.Current.Session[strSessionName] = null;
}
}
}
CookieHelper
-------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
namespace Core.WebHelper
{
/// <summary>
/// Cookie辅助类
/// </summary>
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 = http://www.mamicode.com/cookievalue,
Expires = expires
};
HttpContext.Current.Response.Cookies.Add(cookie);
}
}
}
CacheHelper
------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Collections;
namespace Core.WebHelper
{
/// <summary>
/// 缓存辅助类
/// </summary>
public class CacheHelper
{
/// <summary>
/// 获取数据缓存
/// </summary>
/// <param name="CacheKey">键</param>
public static object GetCache(string CacheKey)
{
System.Web.Caching.Cache objCache = HttpRuntime.Cache;
return objCache[CacheKey];
}
/// <summary>
/// 设置数据缓存
/// </summary>
public static void SetCache(string CacheKey, object objObject)
{
System.Web.Caching.Cache objCache = HttpRuntime.Cache;
objCache.Insert(CacheKey, objObject);
}
/// <summary>
/// 设置数据缓存
/// </summary>
public static void SetCache(string CacheKey, object objObject, TimeSpan Timeout)
{
System.Web.Caching.Cache objCache = HttpRuntime.Cache;
objCache.Insert(CacheKey, objObject, null, DateTime.MaxValue, Timeout, System.Web.Caching.CacheItemPriority.NotRemovable, null);
}
/// <summary>
/// 设置数据缓存
/// </summary>
public static void SetCache(string CacheKey, object objObject, DateTime absoluteExpiration, TimeSpan slidingExpiration)
{
System.Web.Caching.Cache objCache = HttpRuntime.Cache;
objCache.Insert(CacheKey, objObject, null, absoluteExpiration, slidingExpiration);
}
/// <summary>
/// 移除指定数据缓存
/// </summary>
public static void RemoveAllCache(string CacheKey)
{
System.Web.Caching.Cache _cache = HttpRuntime.Cache;
_cache.Remove(CacheKey);
}
/// <summary>
/// 移除全部缓存
/// </summary>
public static void RemoveAllCache()
{
System.Web.Caching.Cache _cache = HttpRuntime.Cache;
IDictionaryEnumerator CacheEnum = _cache.GetEnumerator();
while (CacheEnum.MoveNext())
{
_cache.Remove(CacheEnum.Key.ToString());
}
}
}
}
Tree.cs
-----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Core.WebHelper
{
public class Tree
{
public List<TreeNode> TreeNodes { get; set; }
public string SelectPath { get; set; }//选中路径
bool _ShowLine = true;
public bool ShowLine
{
get { return _ShowLine; }
set { _ShowLine = value; }
}
ShowCheckBoxs _ShowCheckBox = ShowCheckBoxs.None;
public ShowCheckBoxs ShowCheckBox
{
get { return _ShowCheckBox; }
set { _ShowCheckBox = value; }
}
int _ExpentDepth=0;
public int ExpentDepth
{
get{ return _ExpentDepth; }
set{_ExpentDepth = value; }
}
}
public class TreeNode
{
public string TreeNodeID{get;set;}
public string Text { get; set; }
public string Value { get; set; }
NodeDispType _NodeDispType = NodeDispType.Span;//0:不是超链接 1:超链接
public NodeDispType NodeDispType
{
get { return _NodeDispType; }
set { _NodeDispType = value; }
}
public bool? ShowCheckBox { get; set; }
public bool Checked {get;set;}
public string Tag { get; set; }
//public bool? Expanded { get; set; }
public string htmlAttr { get; set; }
public string ParentTreeNodeID { get; set; }
}
public enum ShowCheckBoxs
{
None,
Root,
Parent,
Leaf,
All
}
public enum NodeDispType
{
Alink,
Button,
Span
}
}
WebHelper