Usage of Session object in ASP.NET

后端 未结 5 1465
时光说笑
时光说笑 2021-01-19 04:20

I\'ve just been given a new task to bootstrap a website created by someone else. But I\'m absolutely new to Web. The website is in ASP.NET,C#. The code itself is not hard to

相关标签:
5条回答
  • 2021-01-19 04:57

    ASP.NET session state enables you to store and retrieve values for a user as the user navigates ASP.NET pages in a Web application.

    ASP.NET Session State Overview

    ASP.NET Session State Examples

    0 讨论(0)
  • 2021-01-19 05:00

    Look at, e.g.,

    • ASP.NET Wiki › State Management › Session
    • ASP.NET Session State Overview
    • the documentation for the HttpContext.Session Property
    0 讨论(0)
  • 2021-01-19 05:04
    (string)Session["connSTR"] // Return value from session and casting to string
    

    You need to understand Session, check this ASP.NET Session State Overview

    0 讨论(0)
  • 2021-01-19 05:05

    Session is used to store data for the user's session on the web site. (this data store is per-user-browser session, and is subject to being wiped at any time by various application events)

    It is generally used to store information across multiple page views in a user's session (ie. visit) to your website.

    It can be used anywhere in code that runs in the context of the user's session; meaning inside a page, or in the appropriate application lifecycle events which run in the context of a session (such as Session Start)

    As for your samples;

    The first one, I can't fully explain, as I do not know what the function sh.pensDec() is supposed to do.

    The second one is checking to make sure there is a value stored in that session variable, before running the code that follows.

    0 讨论(0)
  • 2021-01-19 05:09

    HTTP by nature is stateless. The WebServer doesn't know any details after it processes the request and sends back to the client. Thus, any subsequent requests are like fresh requests to the server.

    To Enable the Server to remember & subsequently recognize what it served to the client, ASP.NET uses various mechanisms of which Session is one of them.

    Session is created per user. So, in your Page, you are fetching the "connSTR" are storing it. Whenever a subsequent request comes from the same user, by querying Session with the key

    Session["connSTR"]
    

    you get back its value. Since Session is an Object, its casted as a string in your code.

    0 讨论(0)
提交回复
热议问题