title: ASP.NET CORE Session Alternatives
body: <p>I have an ASP.NET WebForms application which works extensively with Sessions. In fact, i have a model called Questionnaire</p>
  
  <pre><code>public class myQuestionnaire
  {
          public int ID { get; set; }
          public string Name { get; set; }
          public string Status { get; set; } = "A";
          public List&lt;Question&gt; Questions { get; set; } = new List&lt;sQuestion&gt;();
  }
  </code></pre>
  
  <p>The questionnaire contains a list of questions </p>
  
  <pre><code>public class Question
  {
          public int ID { get; set; }
          public string Name { get; set; } = "";
          public string Remarks { get; set; } = "";
          public List&lt;QuestionAnswer&gt; Answers { get; set; } = new List&lt;QuestionAnswer&gt;();
  }
  
  public class QuestionAnswer
  {
          public int ID { get; set; }
          public string Name { get; set; }
          public string Text { get; set; }
          public int Weight { get; set; }
  }
  </code></pre>
  
  <p>I have a maintenance form in which i load the Questionnaire from database, i keep it in a session variable and for every change i need to perform on the questionnaire i load it from Session instead of loading every time from the DB.</p>
  
  <p>To change the Questionnaire Name for example, i have the following web method which is triggered through javascript when the questionnaire name on the form is changed</p>
  
  <pre><code>
  [WebMethod(EnableSession = true)]
  public void ChangeName(string new_name)
  {
      myQuestionnaire _q = () Session["MyQuestionnaireSessionName"]; 
      _q.Name = HttpUtility.UrlDecode(name);
  }
  </code></pre>
  
  <p>Now we decide to migrate the solution in .NET Core but we are facing some issues with the above approach. Actually, there is no way to keep an object in session without serialize it to string. If we do that then we can't access the object directly so we need to manually serialize it and deserialize  it on every change we perform on the object.</p>
  
  <p>The second question is whether there is an alternative way to implement the above case without keeping the entire object on the database for every temporary change.
  We are thinking of cookies, cache and viewstate but i am not familiar enough with MVC and CORE and i don't know the pros and cons of each.</p>