Devin Burke Devin Burke - 1 year ago 52
C# Question

Does static reference to HttpContext.Current.Session return same session for all users?

Is there room for issue in the following code in terms of multiple users of the same web application? I mean, I know that a purely static string will be shared across all sessions for a single ASP.NET application, but given that this explicitly refers to the

, even though it is static it seems like it would always refer to the session instance of the "current user."

But an error is happening that could be explained by everyone sharing the current value of
and thus the most recent change overwriting everyone else's mode value.

(As a background: This string is in a
class that is used throughout the application. I do not want to make hard-coded references to
throughout the application and do not want to have to pass
in every method call from an aspx.cs page.)

public static string Mode
var value = HttpContext.Current.Session["Mode"];
return (value ?? string.Empty).ToString();
HttpContext.Current.Session["Mode"] = value;

Answer Source

HttpContext.Current always returns the context of the current request (if there is a current request).

Since each user will be executing a different request, each context will be different.