我有以下會話變量:Session["UserId"];
如何在對象中保存會話變量?
如何將該變量保存在類和公共變量中?像這樣:
public class UserDC
{
//public static Session UserId = Session["UserId"]
}
我只想打電話給:UserDC.UserId
。
我有以下會話變量:Session["UserId"];
如何在對象中保存會話變量?
如何將該變量保存在類和公共變量中?像這樣:
public class UserDC
{
//public static Session UserId = Session["UserId"]
}
我只想打電話給:UserDC.UserId
。
這是你在找什麼?
public class UserDC
{
public static string UserId
{
get
{
if(HttpContext.Current.Session["Test"] != null)
return HttpContext.Current.Session["Test"].ToString()
else
return "";
}
set
{
HttpContext.Current.Session["Test"] = value;
}
}
}
編輯:
爲了一個靜態屬性或靜態方法中獲取一個會話變量,你必須實際執行以下操作,因爲HttpContext.Current
是靜態的:
HttpContext.Current.Session
public static string UserId
{
get
{
return (string)Session["UserId"];
}
}
你忘了在C#(或VB)中使用屬性的概念嗎? – Lion
請不要用「C#:」等來標題。這就是標籤的用途。 –