My controller is in vb.net. I need to be able to access session variables and enable session state. Right now if I type httpcontext.current.session it is underlined in red. I have done some reading on this, but I'm unclear of the exact steps. I believe
the global.asax page has to be modified. Do I also need to create some class files ? If so, where do they go?
I read an article and here is some code. Can someone please clarify how this is implemented?
Public Class AspApplication
Inherits System.Web.HttpApplication
Protected Sub Application_PostAuthorizeRequest()
If IsWebApiRequest() Then
HttpContext.Current.SetSessionStateBehavior(SessionStateBehavior.Required)
End If
End Sub
Private Function IsWebApiRequest() As Boolean
Return HttpContext.Current.Request.AppRelativeCurrentExecutionFilePath.StartsWith(WebApiConfig.UrlPrefixRelative)
End Function
End Class
Public Shared Sub Register(ByVal routes As RouteCollection)
routes.MapHttpRoute(name:="DefaultApi", routeTemplate:=WebApiConfig.UrlPrefix & "/{controller}/{id}", defaults:=New With {Key
.id = RouteParameter.[Optional]
})
End Sub
And then call the following in Application_Start:
WebApiConfig.Register(RouteTable.Routes);
HttpContext.Session.Add("test1", "1")
Dim test1 = HttpContext.Session("test1")
Or
Session("test2") = "2"
Dim test2 = Session("test2")
Here is the result.
Best Regards,
YihuiSun
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
if you are just starting with MVC, you should use C# or F#, as VB will not be supported with future versions. You also might want to look at asp.net core rather than starting with an end of life product.
Member
247 Points
1310 Posts
Adding Session variables in a Controller page
Jun 07, 2020 10:58 PM|sking|LINK
My controller is in vb.net. I need to be able to access session variables and enable session state. Right now if I type httpcontext.current.session it is underlined in red. I have done some reading on this, but I'm unclear of the exact steps. I believe the global.asax page has to be modified. Do I also need to create some class files ? If so, where do they go?
I read an article and here is some code. Can someone please clarify how this is implemented?
California Mortgage | California refinance
Contributor
2770 Points
789 Posts
Re: Adding Session variables in a Controller page
Jun 08, 2020 10:50 AM|YihuiSun|LINK
Hi, sking
There are two ways to add a session.
Or
Here is the result.
Best Regards,
YihuiSun
All-Star
58254 Points
15674 Posts
Re: Adding Session variables in a Controller page
Jun 08, 2020 03:06 PM|bruce (sqlwork.com)|LINK
if you are just starting with MVC, you should use C# or F#, as VB will not be supported with future versions. You also might want to look at asp.net core rather than starting with an end of life product.
Member
247 Points
1310 Posts
Re: Adding Session variables in a Controller page
Jul 04, 2020 04:35 AM|sking|LINK
I have it working. Thanks for the suggestion.
California Mortgage | California refinance