Answers for "sessions c# mvc"

C#
1

c# access session in class

public class MySession
{
    // private constructor
    private MySession()
    {
      Property1 = "default value";
    }

    // Gets the current session.
    public static MySession Current
    {
      get
      {
        MySession session =
          (MySession)HttpContext.Current.Session["__MySession__"];
        if (session == null)
        {
          session = new MySession();
          HttpContext.Current.Session["__MySession__"] = session;
        }
        return session;
      }
    }

    // **** add your session properties here, e.g like this:
    public string Property1 { get; set; }
    public DateTime MyDate { get; set; }
    public int LoginId { get; set; }
}
Posted by: Guest on April-16-2020
0

Using Session in ASP.NET MVC

private void btnSubmit_Click(object sender, System.EventArgs e)
{ if(IsValid)
  { // Set the Session value.
    Session[txtName.Text] = txtValue.Text;
    // Read and display the value we just set
    lblResult.Text = "The value of <b>" +
	 txtName.Text + "</b> in the Session object is <b>" +
	 Session[txtName.Text].ToString() + "</b>"; } }
Posted by: Guest on May-04-2021

C# Answers by Framework

Browse Popular Code Answers by Language