C implements simple access and setup of Session classes

  • 2021-01-22 05:21:00
  • OfStack

This article illustrates C#'s implementation of simply getting and setting up Session classes. Share with you for your reference. The specific analysis is as follows:

This is a simple C# to get Session and set Session files. This class mainly implements the two most commonly used functions:

1, GetSession(string name);
2, SetSession(string name, object val) Set session

The specific code is as follows:


using System.Web;
namespace DotNet.Utilities
{
  /// <summary>
  /// Session  Action class 
  /// 1 , GetSession(string name) According to the session Name for session object 
  /// 2 , SetSession(string name, object val) Set up the session
  /// </summary>
  public class SessionHelper
  {
    /// <summary>
    ///  According to the session Name for session object 
    /// </summary>
    /// <param name="name"></param>
    /// <returns></returns>
    public static object GetSession(string name)
    {
      return HttpContext.Current.Session[name];
    }
    /// <summary>
    ///  Set up the session
    /// </summary>
    /// <param name="name">session  The name </param>
    /// <param name="val">session  value </param>
    public static void SetSession(string name, object val)
    {
      HttpContext.Current.Session.Remove(name);
      HttpContext.Current.Session.Add(name, val);
    }
  }
}

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


Related articles: