C operates on class instances of session

  • 2021-01-25 07:51:07
  • OfStack

This article illustrates C# classes that operate on session. Share with you for your reference. The specific analysis is as follows:

This class can set values to arrays or read values to lists of arrays. If you need to, you can use this class to extend the C# class yourself.


using System.Web;
namespace DotNet.Utilities
{
 public static class SessionHelper2
 {
  /// <summary>
  ///  add Session , the transfer is valid for 20 minutes 
  /// </summary>
  /// <param name="strSessionName">Session The name of the object </param>
  /// <param name="strValue">Session value </param>
  public static void Add(string strSessionName, string strValue)
  {
   HttpContext.Current.Session[strSessionName] = strValue;
   HttpContext.Current.Session.Timeout = 20;
  }
  /// <summary>
  ///  add Session , the transfer is valid for 20 minutes 
  /// </summary>
  /// <param name="strSessionName">Session The name of the object </param>
  /// <param name="strValues">Session An array of values </param>
  public static void Adds(string strSessionName, string[] strValues)
  {
   HttpContext.Current.Session[strSessionName] = strValues;
   HttpContext.Current.Session.Timeout = 20;
  }
  /// <summary>
  ///  add Session
  /// </summary>
  /// <param name="strSessionName">Session The name of the object </param>
  /// <param name="strValue">Session value </param>
  /// <param name="iExpires"> Validity of Transfer (minutes) </param>
  public static void Add(string strSessionName, string strValue, int iExpires)
  {
   HttpContext.Current.Session[strSessionName] = strValue;
   HttpContext.Current.Session.Timeout = iExpires;
  }
  /// <summary>
  ///  add Session
  /// </summary>
  /// <param name="strSessionName">Session The name of the object </param>
  /// <param name="strValues">Session An array of values </param>
  /// <param name="iExpires"> Validity of Transfer (minutes) </param>
  public static void Adds(string strSessionName, string[] strValues, int iExpires)
  {
   HttpContext.Current.Session[strSessionName] = strValues;
   HttpContext.Current.Session.Timeout = iExpires;
  }
  /// <summary>
  ///  Read a Session Object values 
  /// </summary>
  /// <param name="strSessionName">Session The name of the object </param>
  /// <returns>Session Object values </returns>
  public static string Get(string strSessionName)
  {
   if (HttpContext.Current.Session[strSessionName] == null)
   {
    return null;
   }
   else
   {
    return HttpContext.Current.Session[strSessionName].ToString();
   }
  }
  /// <summary>
  ///  Read a Session Array of object values 
  /// </summary>
  /// <param name="strSessionName">Session The name of the object </param>
  /// <returns>Session Array of object values </returns>
  public static string[] Gets(string strSessionName)
  {
   if (HttpContext.Current.Session[strSessionName] == null)
   {
    return null;
   }
   else
   {
    return (string[])HttpContext.Current.Session[strSessionName];
   }
  }
  /// <summary>
  ///  Delete a Session object 
  /// </summary>
  /// <param name="strSessionName">Session The name of the object </param>
  public static void Del(string strSessionName)
  {
   HttpContext.Current.Session[strSessionName] = null;
  }
 }
}

I hope this article is helpful to your C# program design.


Related articles: