繁体   English   中英

如何从C#中的字典类型SESSION变量获取值

[英]How to get the values from Dictionary type SESSION Variable in C#

我正在使用C#。

我在SESSION变量[“ FROMDATA”]中得到了以下格式值,我正在使用字典来存储表单过帐数据。 请参阅相关问题

以下是我的SESSION变量中的一些值。

1) key - "skywardsNumber" value-"99999039t"
2) key - "password" value-"a2222222"
3) key - "ctl00$MainContent$ctl22$FlightSchedules1$ddlDepartureAirport-suggest" value-"London"
4) key - "ctl00$MainContent$ctl22$ctl07$txtPromoCode" value-"AEEGIT9"
.
.
....so on

现在,我想用METHOD 创建一个CLASS ,在其中我将只传递“ KEY” ,它将首先检查它的NULL或EMPTY ,然后它将从SESSION变量[“ FROMDATA”]返回其值

请建议使用C#

尝试这个,

public class Test
{
    public static string GetValue(string key)
    {
        string value = string.Empty;
        if (HttpContext.Current.Session["FROMDATA"] != null)
        {
            Dictionary<string, string> form = (Dictionary<string, string>)HttpContext.Current.Session["FROMDATA"];
            if(form.ContainsKey(key))
                value = form[key];
        }
        return value;
    }
}

编辑:

 public static string GetValue(string sessionkey,string key)
    {
        string value = string.Empty;
        if (HttpContext.Current.Session[sessionkey] != null)
        {
            Dictionary<string, string> form = (Dictionary<string, string>)HttpContext.Current.Session[sessionkey];
            if(form.ContainsKey(key))
                value = form[key];
        }
        return value;
    }

试试这个:您必须对其稍作调整才能使其符合要求。 但是你会得到基本的想法

public class Session
    {        

        private const string _skyWardsNumber = "skyWardsNumber";
        // Add other keys here

        public string SkyWardsNumber
        {
            get
            {
                object str = (ReadFromDictionary(_skyWardsNumber);
                if (str != null)
                {
                    return (string) str;
                }
                else
                {
                    return string.Empty;
                }

            }
            set
            {
                WriteToDictionary(_skyWardsNumber, value);
            }
        }

        public object ReadFromDictionary(string key)
        {
            IDictionary dictionary = (ReadFromContext("Dictionary") as IDictionary);
            if (dictionary != null && dictionary.ContainsKey(key))
            {
               return dictionary[key];
            }
            else
            {
               return null;
            }
        }

        public object WriteFromDictionary(string key, object value)
        {
            IDictionary dictionary = (ReadFromContext("Dictionary") as IDictionary);

            if(dictionary == null)
                 WriteToContext("Dictionary", new Dictionary<string, string>())

            dictionary = (ReadFromContext("Dictionary") as IDictionary);

            if (dictionary.ContainsKey(key))
            {
               dictionary[key] = value;
            }
            else
            {
               dictionary.Add(//add new keyvaluepair.);
            }
        }

        private static void WriteToContext(string key, object value)
        {
            HttpContext.Current.Session[key] = value;
        }


        private static object ReadFromContext(string key)
        {
            if(HttpContext.Current.Session[key] != null)
                return HttpContext.Current.Session[key] as object;

             return null;
        }

    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM