C#操作session的类实例
作者:feige 时间:2023-07-17 13:57:19
本文实例讲述了C#操作session的类。分享给大家供大家参考。具体分析如下:
这个C#类对session操作进行了再次封装,可以大大简化session的常用操作,同时这个类可以将session值设置为数组,也可以将值读取为数组列表,如果你有这方面的需要可以使用这个类,扩这自己对这个C#类进行扩展。
using System.Web;
namespace DotNet.Utilities
{
public static class SessionHelper2
{
/// <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 string Get(string strSessionName)
{
if (HttpContext.Current.Session[strSessionName] == null)
{
return null;
}
else
{
return HttpContext.Current.Session[strSessionName].ToString();
}
}
/// <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;
}
}
}
希望本文所述对大家的C#程序设计有所帮助。
标签:C#,session,类
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
利用json2POJO with Lombok 插件自动生成java类的操作
2023-07-12 09:31:28
![](https://img.aspxhome.com/file/2023/8/69718_0s.jpg)
Spring Security入门demo案例
2023-07-01 18:57:30
Springboot通过run启动web应用的方法
2021-08-30 01:57:37
![](https://img.aspxhome.com/file/2023/7/64527_0s.png)
深入理解C++中public、protected及private用法
2023-07-02 11:30:17
Java中集合和数组的排序方式小结
2023-08-25 00:52:10
如何在c语言下关闭socket
2021-12-26 10:22:46
详解Java Proxy动态 代理机制
2023-07-24 21:01:58
![](https://img.aspxhome.com/file/2023/6/57726_0s.png)
Java class文件格式之属性_动力节点Java学院整理
2021-06-13 01:52:00
![](https://img.aspxhome.com/file/2023/4/101944_0s.png)
Android中JSON的4种解析方式使用和对比
2023-05-03 11:32:10
单点登录的三种方式和JWT的介绍与使用
2023-05-19 22:10:59
Flutter实现顶部导航栏功能
2023-03-10 17:13:48
![](https://img.aspxhome.com/file/2023/2/130442_0s.jpg)
Android Intent的几种用法详细解析
2023-08-23 20:01:01
Java TCP协议通信超详细讲解
2021-05-30 20:53:35
![](https://img.aspxhome.com/file/2023/7/131977_0s.png)
SpringBoot自动配置实现流程详细分析
2023-06-23 13:35:45
![](https://img.aspxhome.com/file/2023/0/69940_0s.png)
android开发之为activity增加左右手势识别示例
2021-09-30 12:41:50
8个Spring事务失效场景详解
2021-07-25 02:45:38
![](https://img.aspxhome.com/file/2023/4/113254_0s.png)
最简单易懂的java数组排序方法整理
2023-01-03 18:56:41
Java实现五子棋游戏的完整代码
2022-07-01 15:32:34
![](https://img.aspxhome.com/file/2023/8/62328_0s.jpg)
Java监听器ActionListener与MouseListener的执行顺序说明
2022-02-04 20:08:23
![](https://img.aspxhome.com/file/2023/9/72629_0s.png)
C#中控件动态添加事件绑定的时机详解
2022-05-05 17:40:59