如何设置字典类型会话变量中的值?
我有下面的代码来获取会话变量值的字典类型。请参阅下面的代码
在我的代码中,我只是使用下面的代码从会话变量中获取任何值:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
除了
value = form[key];
行之外,“SetValue”几乎相同。这应该变成form[key] = value;
。无需“将字典设置回会话”,因为对同一字典的引用仍然存在于会话中。
示例:
设置值
删除值:
The "SetValue" would be almost identical, except for the line
value = form[key];
. That should becomeform[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
Removing a value: