• .NET Session操作


      1 public class SessionHelper
      2     {
      3         /// <summary>
      4         /// 根据session名获取session对象
      5         /// </summary>
      6         /// <param name="name"></param>
      7         /// <returns></returns>
      8         public static object GetSession(string name)
      9         {
     10             return HttpContext.Current.Session[name];
     11         }
     12         /// <summary>
     13         /// 设置session
     14         /// </summary>
     15         /// <param name="name">session 名</param>
     16         /// <param name="val">session 值</param>
     17         public static void SetSession(string name, object val)
     18         {
     19             HttpContext.Current.Session.Remove(name);
     20             HttpContext.Current.Session.Add(name, val);
     21         }
     22         /// <summary>
     23         /// 添加Session,调动有效期为20分钟
     24         /// </summary>
     25         /// <param name="strSessionName">Session对象名称</param>
     26         /// <param name="strValue">Session值</param>
     27         public static void Add(string strSessionName, string strValue)
     28         {
     29             HttpContext.Current.Session[strSessionName] = strValue;
     30             HttpContext.Current.Session.Timeout = 20;
     31         }
     32 
     33         /// <summary>
     34         /// 添加Session,调动有效期为20分钟
     35         /// </summary>
     36         /// <param name="strSessionName">Session对象名称</param>
     37         /// <param name="strValues">Session值数组</param>
     38         public static void Adds(string strSessionName, string[] strValues)
     39         {
     40             HttpContext.Current.Session[strSessionName] = strValues;
     41             HttpContext.Current.Session.Timeout = 20;
     42         }
     43 
     44         /// <summary>
     45         /// 添加Session
     46         /// </summary>
     47         /// <param name="strSessionName">Session对象名称</param>
     48         /// <param name="strValue">Session值</param>
     49         /// <param name="iExpires">调动有效期(分钟)</param>
     50         public static void Add(string strSessionName, string strValue, int iExpires)
     51         {
     52             HttpContext.Current.Session[strSessionName] = strValue;
     53             HttpContext.Current.Session.Timeout = iExpires;
     54         }
     55 
     56         /// <summary>
     57         /// 添加Session
     58         /// </summary>
     59         /// <param name="strSessionName">Session对象名称</param>
     60         /// <param name="strValues">Session值数组</param>
     61         /// <param name="iExpires">调动有效期(分钟)</param>
     62         public static void Adds(string strSessionName, string[] strValues, int iExpires)
     63         {
     64             HttpContext.Current.Session[strSessionName] = strValues;
     65             HttpContext.Current.Session.Timeout = iExpires;
     66         }
     67 
     68         /// <summary>
     69         /// 读取某个Session对象值
     70         /// </summary>
     71         /// <param name="strSessionName">Session对象名称</param>
     72         /// <returns>Session对象值</returns>
     73         public static string Get(string strSessionName)
     74         {
     75             if (HttpContext.Current.Session[strSessionName] == null)
     76             {
     77                 return null;
     78             }
     79             else
     80             {
     81                 return HttpContext.Current.Session[strSessionName].ToString();
     82             }
     83         }
     84 
     85         public static void Set<T>(string strSessionName, T value) where T : class , new()
     86         {
     87             HttpContext.Current.Session[strSessionName] = value;
     88             HttpContext.Current.Session.Timeout = 24 * 60;
     89         }
     90 
     91         /// <summary>
     92         /// 读取某个Session对象值
     93         /// </summary>
     94         /// <param name="strSessionName">Session对象名称</param>
     95         /// <returns>Session对象值</returns>
     96         public static T Get<T>(string strSessionName) where T : class , new()
     97         {
     98             if (HttpContext.Current == null || HttpContext.Current.Session == null || HttpContext.Current.Session[strSessionName] == null)
     99             {
    100                 return null;
    101             }
    102             else
    103             {
    104                 return HttpContext.Current.Session[strSessionName] as T;
    105             }
    106         }
    107 
    108         /// <summary>
    109         /// 读取某个Session对象值数组
    110         /// </summary>
    111         /// <param name="strSessionName">Session对象名称</param>
    112         /// <returns>Session对象值数组</returns>
    113         public static string[] Gets(string strSessionName)
    114         {
    115             if (HttpContext.Current.Session[strSessionName] == null)
    116             {
    117                 return null;
    118             }
    119             else
    120             {
    121                 return (string[])HttpContext.Current.Session[strSessionName];
    122             }
    123         }
    124 
    125         /// <summary>
    126         /// 删除某个Session对象
    127         /// </summary>
    128         /// <param name="strSessionName">Session对象名称</param>
    129         public static void Del(string strSessionName)
    130         {
    131             HttpContext.Current.Session[strSessionName] = null;
    132         }
    133     }
  • 相关阅读:
    软件开发模式
    my parnter code review
    官僚模式和功能团队模式的优缺点
    思考题
    my code review
    四则运算
    Android Studio下使用Junit框架测试数组和
    SQL2008 存储过程 增删改查例子
    NET 无法显示XML页怎么办
    vs2010 2005 2008 代码前面出现··········取消方法
  • 原文地址:https://www.cnblogs.com/toloe/p/4630357.html
Copyright © 2020-2023  润新知