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

Man*_*ngh 0 c# asp.net

我正在使用C#.

我在SESSION变量["FROMDATA"]中获得了以下格式值,我使用DICTIONARY来存储FORM Posted Data.请参阅相关问题.

以下是我的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
Run Code Online (Sandbox Code Playgroud)

现在我想在其中创建一个带有METHOD 的CLASS,其中我将只传递"KEY",它将首先检查它是否为NULL或EMPTY,然后它将从SESSION变量["FROMDATA"]返回其值.

请建议使用C#.

ada*_*ost 5

试试这个,

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑:

 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;
    }
Run Code Online (Sandbox Code Playgroud)