How do static properties work in an asp.net environment?

前端 未结 4 2061
南旧
南旧 2020-12-03 21:13

If I had a class with a static property that is set when a user loads a particular page, is that static value unique to that users session?

In other words, if a seco

相关标签:
4条回答
  • 2020-12-03 21:18

    No, it's nothing special just because it's asp.net. ASP.NET itself is just a regular .NET assembly collection. If you want to save things per sessions then you should use the session state. If not, be careful since there are many threads that can access your static data. You should read and learn how threads, locks and race conditions work together.

    0 讨论(0)
  • 2020-12-03 21:20

    Static fields and properties are shared across all instances of a class. All of your users will end up sharing the same value.

    The value will be there until the ASP.NET worker process recycles itself (which happens periodically).

    0 讨论(0)
  • 2020-12-03 21:26

    In addition to Bob's answer, there is this exception of course:

    public static object Item {
        get { return HttpContext.Current.Session["some_key"]; }
    }
    
    0 讨论(0)
  • 2020-12-03 21:39

    Statics are unique to the application domain, all users of that application domain will share the same value for each static property. When you see the word static, think "there will only be one instance of this." How long that instance lasts is a separate question, but the short answer is that it is variable.

    If you want to store values specific to the user look into Session State.

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