C# 尝试使用会话变量,询问给定的会话变量是否为';t空
正如底部描述的情况(使用Webworms而不是Mvc) 由于我可能需要“掌握”会话变量的实现,几乎在每个web应用程序中,我构建了一个专用类来帮助我轻松地管理它 方法如下 第一个代码块用于使用,您可以在下一个代码块中看到它所调用的类 我故意使用拼写错误的名字,以避免歧义(没有任何前缀),…这样我就可以更容易地使用intelliSenseC# 尝试使用会话变量,询问给定的会话变量是否为';t空,c#,asp.net,session-variables,code-design,C#,Asp.net,Session Variables,Code Design,正如底部描述的情况(使用Webworms而不是Mvc) 由于我可能需要“掌握”会话变量的实现,几乎在每个web应用程序中,我构建了一个专用类来帮助我轻松地管理它 方法如下 第一个代码块用于使用,您可以在下一个代码块中看到它所调用的类 我故意使用拼写错误的名字,以避免歧义(没有任何前缀),…这样我就可以更容易地使用intelliSense //“导入”当前项目的类 使用SeSn=Debug_Tests.seseeions.SeSn; //创建对象(通常名称与currentProject相关) pu
//“导入”当前项目的类
使用SeSn=Debug_Tests.seseeions.SeSn;
//创建对象(通常名称与currentProject相关)
public static SeSn.createCurrentSesionVariablesStructNamed CurSesVarStruct=new seseseions.SeSn.createCurrentSesionVariablesStructNamed();
//这个长长的名字在这个小小的“混乱”中帮助了我
到目前为止,这是一个结构
的实例,即“分组”或“捆绑”所有我的全局
作为一个捆绑包
因此,每当我需要存储全局变量时,我都会将值赋给相应的结构变量CurSesVarStruct
必须提供的值
然后,我只需要访问一次会话变量,只需提取“变量集合”——对象
。。。因为它实际上是一个会话变量,所以我将其名称保持不变-\u CurrentSesionGlobals
我尽力描述背景,但简而言之:
它是作为会话变量之一存储在会话中的结构-数据类型=object
或者您可以说在会话之间保存结构的克隆
因此,要使用\u CurrentSesionGlobals
,我可以通过会话访问我需要的任何值,如下所示:
在将结构存储到会话中之前分配它:
CurSesVarStruct.SelectedUercustid=custID;
然后,下面的下一个方法-extractsenvar()
允许我使用以下示例:
提取上次会话中保存的变量:
custID=extractsenvar();
因此,SelectedUercustid
实际上是结构成员之一
问题
从会话变量中提取\u CurrentSesionGlobals
public static SeSn.createCurrentSesionVariablesStructNamed ExtractSesnvar()
{
var currentAppGlobals=SeSn.GetValueAS.acloneofStructureObj(“_CurrentSesionGlobals”);
返回(SeSn.createCurrentSesionVariablesStructNamed)currencAppGlobals;
//问题是指这个位置。
}
问题
如何为null
结果设置返回值
或者首先询问我试图提取的对象
/给定的会话变量
是否为null
,或者是否不存在的条件
因为现在有一个异常错误,当我试图获取值时。。。一个不存在的会话变量
下一个代码块
它是我添加到解决方案中的一个类,作为每个网站应用程序的助手
它实际上是一个名称空间。
因此负责处理会话变量的类是Sesn
namespace Seseions
{
public class Sesn
{
public static bool isNotEmpty()
{
return HttpContext.Current.Session.Keys.Count > 0;
}
public struct CreatCurrentSesionVariablsStructNamed
{
// some of commonly used variables- still testing options..
public int ManagerCustID;
public int SelectedUercustid;
public int recordID;
public int SelectedMonth;
public int SelectedChosenWorker;
public int SelectedYear ;
public string SelectedTable;
public string SelectedColumn;
public string SqlSelectCommandLastQuery;
public string TableOfUsersReference;
public List<string> Fontlist { get; set; }
}
// converts and extract values of session variables
public class GetValueAS
{
public static CreatCurrentSesionVariablsStructNamed ACloneOfTheStructObj(string currntProjectSesVarStructName)
{
if(HttpContext.Current.Session[currntProjectSesVarStructName] != null)
return (CreatCurrentSesionVariablsStructNamed)HttpContext.Current.Session[currntProjectSesVarStructName];
}
public static int _Int(string SesParameterValToReturn)
{
return Convert.ToInt32(HttpContext.Current.Session[SesParameterValToReturn]);
}
public static string _String(string SesParameterValToReturn)
{
return Convert.ToString(HttpContext.Current.Session[SesParameterValToReturn]);
}
public static DataSet _DataSet(string SesParameterValToReturn)
{
return (DataSet)HttpContext.Current.Session[SesParameterValToReturn];
}
public static DataTable _DataTable(string SesParameterValToReturn)
{
return (DataTable)HttpContext.Current.Session[SesParameterValToReturn];
}
public static bool _Bool(string SeSnVarToCheckOn)
{
if (HttpContext.Current.Session[SeSnVarToCheckOn] == null)
return false;
return (bool)HttpContext.Current.Session[SeSnVarToCheckOn];
}
}
// an easy way to access and mange session variables actions
public enum Act
{
Add, Remove, Replace
}
public static void Modify(Act action, string New_SesnVarName= null, object NewP_Value=null, string Currnt_Ses_SesnVarName=null)
{
switch (action)
{
case Act.Remove:
if (isNotEmpty())
HttpContext.Current.Session.Remove(CurSes_ParamName);
break;
case Act.Replace:
HttpContext.Current.Session.Remove(CurSes_ParamName);
HttpContext.Current.Session.Add(New_SesnVarName, NewP_Value);
break;
case Act.Add:
HttpContext.Current.Session.Add(NewQs_SesnVarName, NewP_Value);
break;
}
}
}
}
名称空间Seseions
{
公共类Sesn
{
公共静态bool是notempty()
{
返回HttpContext.Current.Session.Keys.Count>0;
}
公共结构CreatCurrentSesionVariablesStructNamed
{
//一些常用变量-仍然是测试选项。。
公共内部管理系统;
公共国际选择教育委员会;
公共int-recordID;
公共int选择月;
公共int-SelectedChosenWorker;
公共国际选定年;
可选择的公共字符串;
公共字符串SelectedColumn;
公共字符串SqlSelectCommandLastQuery;
用户引用的公共字符串表;
公共列表Fontlist{get;set;}
}
//转换和提取会话变量的值
公共类GetValueAS
{
公共静态CreatCurrentSesionVariablesStructNamed AcloneofStructObj(字符串CurrentProjectSesVarStructName)
{
if(HttpContext.Current.Session[currentprojectsesvarstructname]!=null)
返回(CreatCurrentSesionVariablesStructNamed)HttpContext.Current.Session[CurrentProjectSesVarStructName];
}
公共静态int_int(字符串sesParameterValtoreReturn)
{
返回Convert.ToInt32(HttpContext.Current.Session[sesParameterValtoreReturn]);
}
公共静态字符串_字符串(字符串seParameterValtoreReturn)
{
返回Convert.ToString(HttpContext.Current.Session[SesParameterValToReturn]);
}
公共静态数据集\u数据集(字符串sesParameterValtoreReturn)
{
返回(数据集)HttpContext.Current.Session[sesParameterValtoreReturn];
}
公共静态数据表\u数据表(字符串sesParameterValtoreReturn)
{
return(DataTable)HttpContext.Current.Session[SesParameterValToReturn];
}
公共静态布尔(字符串SeSnVarToCheckOn)
{
if(HttpContext.Current.Session[SeSnVarToCheckOn]==null)
返回false;
返回(bool)HttpContext.Current.Se
public static class SessionKeys
{
public const string ManagerCustID = "ManagerCustID";
...
}