会话状态信息无效,可能在ASP.Net中已损坏

Vai*_*mar 5 asp.net session-variables session-state

我正在使用ASP.Net 3.5和C#,开发ID:Visual Studio 2008.当我使用时

Session["FileName1"] = "text1.txt" 
Run Code Online (Sandbox Code Playgroud)

它工作正常,但后来我正在使用

number1=17;
string FileName1="FileName1" + number1.toString(); 
Run Code Online (Sandbox Code Playgroud)

然后设置

Session[FileName1]="text1.txt";
Run Code Online (Sandbox Code Playgroud)

给我运行时错误

会话状态信息无效,可能在System.Web.SessionState.SessionStateItemCollection.Deserializer(BinaryReader reader)中损坏

当我在Session变量中使用字符串时,任何人都可以解决我的问题吗?请记住,它适用于我的开发机器(意味着本地Visual Studio),但是当部署到服务器时,它会提供上述错误.

在此输入图像描述

Nat*_*her 5

在尝试通过Session [FileName1]语法访问它之前,请确保FileName1变量不为null ...

这是一个链接到其他有同样问题的人:http: //forums.asp.net/t/1069600.aspx

这是他的答案:

在代码中,我找到了以下行:

//some code
Session.Add(sessionVarName, sessionVarValue);
//some other code
Run Code Online (Sandbox Code Playgroud)

显然,由于一些脏数据,有一段时间sessionVarName为null.

在这种情况下,Session.Add不会抛出任何异常,如果您的会话模式是"InProc",则没有问题.但是,如果您的会话模式是"SQLServer",则在会话存储的反序列化期间,您将获得我获得的异常.因此,为了过滤掉脏数据,我将代码修改为:

if (sessionVarName != null)
{
  //somecode
  Session.Add(sessionVarName, sessionVarValue);
  //some other code
}  
Run Code Online (Sandbox Code Playgroud)