如何设置字典类型会话变量中的值?

发布于 2024-10-15 14:36:43 字数 1266 浏览 3 评论 0原文

我有下面的代码来获取会话变量值的字典类型。请参阅下面的代码

在我的代码中,我只是使用下面的代码从会话变量中获取任何值:

string panelOpen = SessionDictionary.GetValue("FORMDATA", "panelOpen");

public class SessionDictionary
{
    public static string GetValue(string dictionaryName, string key)
    {
        string value = string.Empty;
        try
        {
            if (HttpContext.Current.Session[dictionaryName] != null)
            {
                Dictionary<string, string> form = (Dictionary<string, string>)HttpContext.Current.Session[dictionaryName];
                if (form.ContainsKey(key))
                {
                    if (!string.IsNullOrEmpty(key))
                    {
                        value = form[key];
                    }
                }
            }
        }
        catch (Exception ex)
        {
            Logger.Error("{0}: Error while checking Session value from Dictionary", ex, "SessionDictionary");
        }
        return value;
    }
}

现在我想编写一个方法来设置特定会话密钥的值,例如

SessionDictionary.SetValue("FORMDATA", "panelOpen") = "First";

现在如果我再次使用下面的代码应该给我“First”作为我的面板打开键。

string panelOpen = SessionDictionary.GetValue("FORMDATA", "panelOpen");

请建议!

I have got below code to GET dictionary type of session variable value. Please see the below code

In my code, I just use below code to get any value from my session variable:

string panelOpen = SessionDictionary.GetValue("FORMDATA", "panelOpen");

public class SessionDictionary
{
    public static string GetValue(string dictionaryName, string key)
    {
        string value = string.Empty;
        try
        {
            if (HttpContext.Current.Session[dictionaryName] != null)
            {
                Dictionary<string, string> form = (Dictionary<string, string>)HttpContext.Current.Session[dictionaryName];
                if (form.ContainsKey(key))
                {
                    if (!string.IsNullOrEmpty(key))
                    {
                        value = form[key];
                    }
                }
            }
        }
        catch (Exception ex)
        {
            Logger.Error("{0}: Error while checking Session value from Dictionary", ex, "SessionDictionary");
        }
        return value;
    }
}

Now I want to write a method to SET the value for particular session key, for example

SessionDictionary.SetValue("FORMDATA", "panelOpen") = "First";

Now if I again go for below code it should give me "First" for my panelOpen key.

string panelOpen = SessionDictionary.GetValue("FORMDATA", "panelOpen");

Please suggest!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦魇绽荼蘼 2024-10-22 14:36:43

除了 value = form[key]; 行之外,“SetValue”几乎相同。这应该变成 form[key] = value;

无需“将字典设置回会话”,因为对同一字典的引用仍然存在于会话中。

示例:

设置值

public static void SetValue(string dictionaryName, string key, string value)
{
  if (!String.IsNullOrEmpty(key))
  {
    try
    {
        if (HttpContext.Current.Session[dictionaryName] != null)
        {
            Dictionary<string, string> form = (Dictionary<string, string>)HttpContext.Current.Session[dictionaryName];
            if (form.ContainsKey(key))
            {
                form[key] = value;
            }
        }
    }
    catch (Exception ex)
    {
        Logger.Error("{0}: Error while checking Session value from Dictionary", ex, "SessionDictionary");
    }
  }
}

删除值:

public static void RemoveValue(string dictionaryName, string key)
{
  if (!String.IsNullOrEmpty(key))
  {
    try
    {
        if (HttpContext.Current.Session[dictionaryName] != null)
        {
            Dictionary<string, string> form = (Dictionary<string, string>)HttpContext.Current.Session[dictionaryName];
            form.Remove(key); // no error if key didn't exist
        }
    }
    catch (Exception ex)
    {
        Logger.Error("{0}: Error while checking Session value from Dictionary", ex, "SessionDictionary");
    }
  }
}

The "SetValue" would be almost identical, except for the line value = form[key];. That should become form[key] = value;.

No need to "set the dictionary back into the session" as the reference to that same dictionary is still present in the session.

Examples:

Setting a value

public static void SetValue(string dictionaryName, string key, string value)
{
  if (!String.IsNullOrEmpty(key))
  {
    try
    {
        if (HttpContext.Current.Session[dictionaryName] != null)
        {
            Dictionary<string, string> form = (Dictionary<string, string>)HttpContext.Current.Session[dictionaryName];
            if (form.ContainsKey(key))
            {
                form[key] = value;
            }
        }
    }
    catch (Exception ex)
    {
        Logger.Error("{0}: Error while checking Session value from Dictionary", ex, "SessionDictionary");
    }
  }
}

Removing a value:

public static void RemoveValue(string dictionaryName, string key)
{
  if (!String.IsNullOrEmpty(key))
  {
    try
    {
        if (HttpContext.Current.Session[dictionaryName] != null)
        {
            Dictionary<string, string> form = (Dictionary<string, string>)HttpContext.Current.Session[dictionaryName];
            form.Remove(key); // no error if key didn't exist
        }
    }
    catch (Exception ex)
    {
        Logger.Error("{0}: Error while checking Session value from Dictionary", ex, "SessionDictionary");
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文