如何使用.cs文件中的Session变量 [英] How to use a Session variable from a .cs file
本文介绍了如何使用.cs文件中的Session变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的项目中,我在登录页面中定义了一个 Session
变量。如果我尝试在另一个 .cs 文件中使用它(它没有相应的 .aspx 文件),它会说:名称''会话
''在当前上下文中不存在。
任何人都可以告诉我如何访问 Session
变量。
在此先感谢。
Hi,
In my project, I have a Session
variable defined in a Log-in page. If I try to use it in another .cs file (which doesn''t have a corresponding .aspx file), it says: "The name ''Session
'' does not exist in the current context".
Can anyone please tell me how can I access the Session
variable.
Thanks in advance.
推荐答案
尝试使用
HttpContext.Current.Session["varName"];
它应该可以工作。
It should work.
您尝试访问的属性是会话 [< a href =http://msdn.microsoft.com/en-us/library/system.web.ui.page.session.aspxtarget =_ blanktitle =New Window> ^ ]属性。此属性仅在页面 [ ^ ]类。因此,为了访问您的会话对象,您必须使用相应的页面类。
The property you ar trying to acces is the Session[^] property. This property is only defined in a Page[^] class. So in order to access your session objects you must use a coresponding page class.
<pre lang="cs">using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Web.SessionState;
/// <summary>
/// Summary description for Class1
/// </summary>
///IRequiresSessionState enable session in Class file
public class Class1:IRequiresSessionState
{
public Class1()
{
//
// TODO: Add constructor logic here
//
}
public void getSession(string key)
{
if (HttpContext.Current.Session[key] != null)
{
string sessionID= HttpContext.Current.Session[key].ToString();
}
}
}
这篇关于如何使用.cs文件中的Session变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文