如何利用会话变量






2.82/5 (5投票s)
一篇关于如何利用会话变量的文章。
引言
本文主要讨论如何使用 session
变量。有时,开发人员会忘记记住 Session
变量的名称。例如,在用户登录 Web 应用程序系统的一般情况下,作为开发人员,我们会将 UserID
存储到 session
中。
现在,当需要将多个变量(如 UserID
、UserType
、UserRole
、UserCurrentBalance
等)存储到 session
中时,就会出现问题。在这种情况下,开发人员会根据她/他的命名风格将此信息存储到 session 中。
例如
Session["_UserID"] = // Database information
现在,如果我们需要在运行时从 session
中使用此 UserID
信息,我们通常会这样写
int UserID = (int)Session["_UserID"].ToString();
在这种情况下,我们必须手动将 Session
变量值转换为 String
。
因此,如果我们的 session
变量包含不同类型的信息,如 FullName
和 CurrentBalance
,当我们使用它时,我们必须手动将我们的 session
变量值转换为适当的类型。
我们可以做什么
为了解决这个问题,我引入了一个名为 SessionManagement
的类。此类将根据您对 Session
变量的需求具有属性。
让我们看看如何做到这一点来使用 Session
变量。
Using the Code
我们的第一步是创建 SessionManagement
类。
在您的解决方案资源管理器中添加一个名为 SessionManagement.cs 的新类,并根据您对 Session
变量的需求创建不同的属性。
public class SessionManagement
{
public int UserID
{
get { return HttpContext.Current.Session["UserID"] == null ? 0 :
(int)HttpContext.Current.Session["UserID"]; }
set { HttpContext.Current.Session["UserID"] = value; }
}
public string UserType
{
get { return HttpContext.Current.Session["UserType"] == null ?
string.Empty : HttpContext.Current.Session["UserType"].ToString(); }
set { HttpContext.Current.Session["UserType"] = value; }
}
public decimal UserCurrentBalance
{
get { return HttpContext.Current.Session["UserCurrentBalance"] == null ?
0 : (decimal)HttpContext.Current.Session["UserCurrentBalance"]; }
set { HttpContext.Current.Session["UserCurrentBalance"] = value; }
}
}
在这里,在上面的 SessionManagement.cs 类中,我创建了三个不同类型值的属性:UserID
为 int
,UserType
为 string
,UserCurrentBalance
为 decimal
,因为我想在用户登录时将用户的 UserID
、UserType
和 UserCurrentBalance
存储到 session
中。
我们的第二步是创建一个继承 System.Web.UI.Page
类的 BasePage
,以便我们可以直接在代码中使用我们的 Page session
,并且通过这样做,我们还可以直接使用 SessionManagement
类的属性。
在您的解决方案资源管理器中添加另一个名为 BasePage.cs 的新类,在其中继承 System.Web.UI.Page
类,并以与下面相同的方式创建一个属性:
public class BasePage : System.Web.UI.Page
{
private SessionManagement _SessionManagement;
public SessionManagement SessionManagement
{
get
{
if (Session["SessionManagement"] == null)
{
_SessionManagement = new SessionManagement();
Session["SessionManagement"] = _SessionManagement;
}
else
{
_SessionManagement = Session["SessionManagement"] as
SessionManagement;
}
return _SessionManagement;
}
}
}
创建 BasePage
类后,是时候使用它了。
在您的解决方案资源管理器中添加一个名为 Default.aspx 的新 WebForm
,然后转到此页面的代码隐藏部分。
在您的解决方案资源管理器中添加一个新的 Default.aspx 页面代码隐藏后,您将看到以下内容。
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
从您的页面代码隐藏类中删除继承的类 'System.Web.UI.Page
'。
像这样在 _Default
页面类中继承我们的 BasePage
类
public partial class _Default : BasePage
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
之后,您可以直接将用户数据库信息的值分配给 session,并且您也可以直接使用它。
这是一个例子,说明通过这种机制在用户成功登录时将用户信息存储到 session 中。
如果用户的登录凭据匹配,我将给您一个简单的用户登录示例,并通过我们的 SessionManagement
类将信息存储到 session
中。
protected void btnLogin_Click(object sender, EventArgs e)
{
// This is suppose your UserInformation class to validate
// that user credential is valid or not.
UserInformation objUserInformation = new UserInformation();
objUserInformation.UserID = txtUserID.Text;
objUserInformation.Password = txtPassword.Text;
// If UserID and Password match, we'll store user information
// to the session by SessionManagemenbt class.
if (objUserInformation.IsUserAuthenticated())
{
// This is the actual use of storing values to the session
// by SessionManagement class
this.SessionManagement.UserID = (int)objUserInformation.UserID
this.SessionManagement.UserType = objUserInformation.UserType.ToString();
this.SessionManagement.UserCurrentBalance =
(decimal)objUserInformation.UserCurrentBalance;
}
else
{
lblMessage.Text = "Invalid UserName or Password.
}
}
同样,您可以直接从您的项目的任何页面检索 session
信息,但请记住一件事,该页面必须从 BasePage
继承,而不是从 System.Web.UI.Page
继承。
您可以这样检索一个 Session
变量
int UserID = this.SessionManagement.UserID;
string UserType = this.SessionManagement.UserType;
decimal UserCurrentBalance = this.SessionManagement.UserCurrentBalance;
就这样。希望您喜欢它。
历史
- 2009年1月31日:初始发布