Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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 second user then loads the page and sets the static property, will each user have a distinct value, or will both use the second users value?

like image 887
asawyer Avatar asked Oct 26 '10 18:10

asawyer


People also ask

What are static properties in C#?

In C#, static means something which cannot be instantiated. You cannot create an object of a static class and cannot access static members using an object. C# classes, variables, methods, properties, operators, events, and constructors can be defined as static using the static modifier keyword.

How do I serve a static file in net core?

To serve static files from an ASP.NET Core app, you must configure static files middleware. With static files middleware configured, an ASP.NET Core app will serve all files located in a certain folder (typically /wwwroot).

Is asp net a core MVC?

The ASP.NET Core MVC framework is a lightweight, open source, highly testable presentation framework optimized for use with ASP.NET Core. ASP.NET Core MVC provides a patterns-based way to build dynamic websites that enables a clean separation of concerns.


2 Answers

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.

like image 124
Bob Avatar answered Oct 19 '22 14:10

Bob


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

public static object Item {     get { return HttpContext.Current.Session["some_key"]; } } 
like image 25
Protector one Avatar answered Oct 19 '22 16:10

Protector one