点击下载 SessionHelper.rar

下面是代码大家看一下

这个类主要是关于Session的基本操作
比如:
1.获取Session值
2.设置一个Session的值
3.清空所有的Session
4.删除一个Session
5.删除所有的Session等
具体代码如下所示

/// <summary>
/// 联系方式:361983679
/// 更新网站:[url=http://www.cckan.net/thread-655-1-1.html]http://www.cckan.net/thread-655-1-1.html[/url]
/// </summary>
using System.Web; namespace DotNet.Utilities
{
/// <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
/// </summary>
/// <returns></returns>
public static void ClearSession()
{
HttpContext.Current.Session.Clear();
} /// <summary>
/// 删除一个指定的ession
/// </summary>
/// <param name="name">Session名称</param>
/// <returns></returns>
public static void RemoveSession(string name)
{
HttpContext.Current.Session.Remove(name);
} /// <summary>
/// 删除所有的ession
/// </summary>
/// <returns></returns>
public static void RemoveAllSession(string name)
{
HttpContext.Current.Session.RemoveAll();
}
}
}
05-11 11:30