Search code examples
c#classsessionvarclass-library

how to store and get stored data in to a session in c# if it stored using var


I'm getting data from web service like follow

 string serviceUrl = "https://www.mscholid.com/assings/handlqueryrs/myprod.ashx";
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(serviceUrl);
            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();

            StreamReader sr = new StreamReader(resp.GetResponseStream());
            string result = sr.ReadToEnd();
            sr.Close();


            var rootResult = XElement.Parse(result);

now I want to put this root result into a session

Session["rootv"] = rootResult;

then I want to retrieve it. store function should do inside a class

  public class NileResult
    {


        public dynamic nilecruiseFinalData_Images(string selectedID)
        {
            string serviceUrl = "https://www.mscholid.com/assings/handlqueryrs/myprod.ashx";
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(serviceUrl);
            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();

            StreamReader sr = new StreamReader(resp.GetResponseStream());
            string result = sr.ReadToEnd();
            sr.Close();


            var rootResult = XElement.Parse(result);
//in here  I want to store in to a session
 }
}

how can I do this.


Solution

  • To access the session of the request, you can use:

    HttpContext.Current.Session["rootv"] = rootResult;
    

    HttpContext.Current is the current context of the request.