Is it possible to manipulate the contents of the session variable in C#?

Asked 1 years ago, Updated 1 years ago, 63 views

I have a question about C#Session.
Assume you have stored and retained an array or collection in your session.

List<int>numlist=new List<int>();
Session ["a"] = numlist;

Is it possible to add a value to the numlist in Session ["a"] at this time?
After a lot of research, I found that I could store arrays and collections in a session, but
I don't know how to manipulate the array and collection in the session.

If anyone knows, I would appreciate it if you could let me know.
Thank you for your cooperation.

c# asp.net

2022-09-30 10:31

2 Answers

Looking at the article below, it seems that it cannot be operated directly.
adding string array in existing session of array c#
Using List Objects in a Session Variable

Both articles seem to be created and substituted by themselves if there are no variables in the session, and if there are variables, copy them locally and do the necessary operations before substituting the session.

add

In the case of ASP.NET Core mentioned in the comment, the following article may be helpful.
Setting and Retrieving Session Values—Managing Sessions and States in ASP.NET Core

Use the PageModel class in Razor Pages or the Controller class in MVC with HttpContext.Session to access the session state. This property is an implementation of ISession.
The ISession implementation provides multiple extension methods for setting and retrieving integer and string values. The extension method is located in the Microsoft.AspNetCore.Http namespace.

It seems to implement an extension method with the type Set/Get of ISession to serialize/decrystallize it.

This entire page, including before and after this, probably explains how ASP.NET Core handles sessions.

Related Articles for English Sites:
How to store list object in session variable using asp.net core.And how to fetch values from session variable from the View?
Define and obtain a list of ASP.Net Core (Session)
How to set differential generic list value in same session in asp.net core 3.1


2022-09-30 10:31

I think you can use properties!

publicList<string>NumList{
   get {return(List<string>) Session["a"];}
   set {Session["a"] = value;}
}


2022-09-30 10:31

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.