locked
Assign value to session in Global.asax RRS feed

  • Question

  • User944339287 posted

    Dear all, may i know why HttpContext.Current.Session("pagename") is Nothing?

    <%@ Application Language="VB" %>
    <%@ Import Namespace="System.Web.Routing" %>
    
    <script runat="server">
    
        Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
            
            ' Code that runs on application startup   
            RegisterRoutes(RouteTable.Routes)
                    
        End Sub
           
        Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
            ' Code that runs when a new session is started
            HttpContext.Current.Session("pagename") = "microsoft"
        End Sub
       
        Private Shared Sub RegisterRoutes(ByVal routes As RouteCollection)
    
            Dim pagename As String
            
            If HttpContext.Current IsNot Nothing AndAlso HttpContext.Current.Session IsNot Nothing Then
                pagename = HttpContext.Current.Session("pagename")
            End If
                   
            routes.MapPageRoute("home", "home", "~/pages/" + pagename + "/index.aspx")
            
        End Sub
    
    </script>

    Tuesday, September 8, 2015 7:03 AM

Answers

All replies

  • User475983607 posted

    Session is not available at Application_Start. 

    Tuesday, September 8, 2015 7:30 AM
  • User1738843376 posted

    use:

    Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
        HttpContext.Current.Session("pagename") = "microsoft"
    End Sub

    Tuesday, September 8, 2015 7:33 AM
  • User753101303 posted

    Hi,

    Because your code is called from Application_Start and the session is not yet available (Session_Start is triggered AFTER Application_Start). Is this really a per user value? For now hardcoding the value would make no difference and anyway, as far as I know, routing is global to the app.

    It could be better to explain what you are trying to do and see how others would do it.

    Tuesday, September 8, 2015 8:23 AM
  • User944339287 posted

    Hi,

    First of all, I have numbers of child folder under parent folder [pages]

    http://localhost/home/ is the only URL users able to browse.

    if user A visit http://localhost/home/ , the URL routing will be c:\websites\pages\A\index.aspx

    if user B visit http://localhost/home/ , the URL routing will be c:\websites\pages\B\index.aspx

    * my web application able to verify who is user A and user B.

    Tuesday, September 8, 2015 10:20 AM
  • User614698185 posted

    Hi kengkit,

    * my web application able to verify who is user A and user B.

    Firstly, get session["UserName"] value in the home.apsx.cs page.

    Secondly, add session["UserName"] value to navigation url.

    Thirdly, use Response.Redirect to navigate the web page.

    For more information, please refer to the following links:

    http://stackoverflow.com/questions/21201441/navigateurl-in-asp-net

    http://stackoverflow.com/questions/3394031/how-to-include-session-variable-in-navigateurl-in-hyperlink 

    Best Regards,

    Candice Zhou

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 9, 2015 4:26 AM