簡體   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