是否静态引用HttpContext.Current.Session为所有用户返回相同的会话? [英] Does static reference to HttpContext.Current.Session return same session for all users?

查看:169
本文介绍了是否静态引用HttpContext.Current.Session为所有用户返回相同的会话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有相同的Web应用程序的多个用户而言空间问题,以下code?我的意思是,我知道,一个纯粹的静态字符串将在一个单一的ASP.NET应用程序的所有会话共享,但考虑到这明确指的是 Current.Session ,甚至虽然它是静态的,好像它总是指的会话实例当前用户。

但是,一个错误发生,可以由大家共享模式下的电流值进行解释,因此,最近的更改覆盖别人的模式值。

(作为背景:这个字符串是整个应用程序使用的助手类,我不想做出硬codeD引用会话[模式] 整个应用程序并且不希望有传会话[模式] 每从aspx.cs页面的方法调用。)

 公共静态字符串模式
{
    得到
    {
        VAR值= HttpContext.Current.Session [模式];
        收益率(值??的String.Empty)的ToString();
    }
    组
    {
        HttpContext.Current.Session [模式] =值;
    }
}


解决方案

HttpContext.Current 总是返回当前请求的情况下(如果有电流要求)。

由于每个用户将执行一个不同的请求,每个上下文将是不同的。

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 Current.Session, 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 Mode and thus the most recent change overwriting everyone else's mode value.

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

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

解决方案

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.

这篇关于是否静态引用HttpContext.Current.Session为所有用户返回相同的会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆