Session in .NET Framework 4.8

I am working to convert a .NET Framework 4.8 solution.

The existing application uses Session quite a bit and I need to be able to maintain that throughout the conversion process.

I cannot seem to find a way to access Session[“ObjectName”] to get/set values.

I have followed instructions on: Session and cookies | DotVVM Documentation

I am able to access cookies and that is working, but I cannot figure out how to access the Session object.

How is Session accessed?

Ok, I found I can do this:

HttpContext.Current.Session["ObjName"]

However, that requires using System.Web;

Is there a better way?

Your approach of using the System.Web.HttpContext.Current.Session is correct, or as correct as it gets.

DotVVM does not reference System.Web, it relies on the Owin interface which doesn’t provide access to session (AFAIK). This means there isn’t a way for us to provide any universal abstraction over Asp.Net and Asp.Net Core sessions.

Great. Thank you for the reply. It’s good to know that at least I’m doing it as right as can be.

1 Like

Also note that HttpContext.Current is sensitive to Async methods, so it might be a good idea to store it somewhere (i.e. variable, scoped service) before doing async calls. It would be null, so you’ll notice if run into this issue.