我为一个老系统升级,用.NET,中间设计到Session变量!
我根本无法读取ASP中的Session变量。系统很大!不可能一次全部转完!
各位高手知道该怎么作才能用.NET读取ASP的Session变量呢?

解决方案 »

  1.   

    好像不行...不过你可以支MSDN论坛中去找找.
      

  2.   

    http://www.microsoft.com/china/MSDN/library/WebServices/ASP.NET/HowtoShareSessionStateBetweenClassicASPandASP.NET.mspx
      

  3.   

    ASP.NET实现   在ASP.NET中每一个Web页都衍生自System.Web.UI.Page类。Page类集合了HttpSession对象的一个实例用于处理对话数据。在本例中,叫做SessionPage的自定义Page类来衍生自System.Web.UI.Page,提供了类似Page类的所有特性。唯一的区别是默认的HttpSession使用自定义的对话对象重载了(对实例变量使用new修改符,C#允许衍生的类隐藏基类的成员)。 
    public class SessionPage : System.Web.UI.Page 

     ... 
     public new mySession Session = null; 
     ... 
    }    自定义的对话类使用HybridDictionary对象来相应保存内存中的对话状态(HybridDictionary可用于处理任意数量的对话元素)。为了与传统的ASP通用,该自定义对话对象将限制对话数据类型为字符串型(默认的HttpSession允许对话保存任意类型的数据,不能与传统的ASP通用)。 
    [Serializable] 
    public class mySession  

     private HybridDictionary dic = new HybridDictionary();  public mySession() 
     { 
     }  public string this [string name] 
     { 
      get 
      { 
       return (string)dic[name.ToLower()]; 
      } 
      set 
      { 
       dic[name.ToLower()] = value; 
      } 
     } 
    }  
        Page类为定制暴露了不同的事件和方法。特别是OnInit方法用于设置Page对象的初始化状态。如果请求不包含名为mySession的Cookie,将为请求者产生一个新的mySession Cookie。另外,对话数据将使用自定义数据访问对象SessionPersistence从SQL Server中检索出来。DSN和SessionExpiration的值从web.config中检索。 
    override protected void OnInit(EventArgs e) 

     InitializeComponent(); 
     base.OnInit(e); 

    private void InitializeComponent() 
    {  
     cookie = this.Request.Cookies[sessionPersistence.SessionID];  if (cookie == null) 
     { 
      Session = new mySession(); 
      CreateNewSessionCookie(); 
      IsNewSession = true; 
     } 
     else 
      Session = sessionPersistence.LoadSession( 
        Server.UrlDecode(cookie.Value).ToLower().Trim(),  
        dsn,  
        SessionExpiration 
       );   this.Unload += new EventHandler(this.PersistSession); 

    private void CreateNewSessionCookie() 

     cookie = new HttpCookie(sessionPersistence.SessionID,  
     sessionPersistence.GenerateKey()); 
     this.Response.Cookies.Add(cookie); 

      
      

  4.   

    ASP实现   原来的ASP对话只能将对话数据保存在内存中。为了将对话数据保存到SQL Server,需要写一个自定义的Visual Basic 6.0 COM对象代替现在的对话对象来管理对话状态。该COM对象在每个Web请求开始时被初始化,并从SQL Server重新载入对话数据。ASP脚本完成时,该对象将终止并把对话状态将返回到SQL Server。 
    Visual Basic 6 COM Session对象的主要目的是提供对微软Internet信息服务器(IIS)内部对象的访问。Visual Basic 6 COM对话对象使用SessionUtility组件的mySession类来保存对话状态,SessionUtility的SessionPersistence类用于载入和保存对话数据到SQL Server。使用regasm.exe工具将mySession和 SessionPersistence类作为COM对象暴露。regasm.exe工具可以注册并为COM客户端建立一个类型库来使用框架组件类。   在对象的构造函数中状态信息被重新载入。构造函数(class_initialize)首先从Application对象中检索对话cookie、对话超时设置(SessionTimeOut)、数据库连接字符串(SessionDSN),并建立mySession类的一个实例来保持对话数据。接着构造函数将试图重新使用给定的cookie从SQL Server中载入对话数据。如果SQL Server中没有对话信息,或者对话已经终止,将产生一个新的cookie。如果SQL Server返回了对话状态数据,对话状态信息将保存在mySession对象中。 
    Private Sub Class_Initialize() 
     On Error GoTo ErrHandler: 
     Const METHOD_NAME As String = "Class_Initialize" 
     Set mySessionPersistence = New SessionPersistence 
     Set myObjectContext = GetObjectContext() 
     mySessionID = ReadSessionID() 
     myDSNString = GetConnectionDSN() 
     myTimeOut = GetSessionTimeOut() 
     myIsNewSession = False 
     Call InitContents  Exit Sub 
    ErrHandler: 
     Err.Raise Err.Number, METHOD_NAME & ":" & Err.Source, Err.Description 
    End Sub Private Sub InitContents() 
     On Error GoTo ErrHandler: 
     Const METHOD_NAME As String = "InitContents"  
     If mySessionID = "" Then 
      Set myContentsEntity = New mySession 
      mySessionID = mySessionPersistence.GenerateKey 
      myIsNewSession = True 
     Else 
      Set myContentsEntity =mySessionPersistence.LoadSession(mySessionID, myDSNString,   myTimeOut) 
    End If  Exit Sub 
    ErrHandler: 
     Err.Raise Err.Number, METHOD_NAME & ":" & Err.Source, Err.Description 
    End Sub  
        如果对象实例超出了脚本的范围,将执行解构函数(class_terminate)。解构函数将使用SessionPersistence.SaveSession()方法保持对话数据。如果是一个新对话,解构函数将新cookie发送回浏览器。 
    Private Sub Class_Terminate() 
     On Error GoTo ErrHandler: 
     Const METHOD_NAME As String = "Class_Terminate" 
     Call SetDataForSessionID 
     Exit Sub  
    ErrHandler: 
     Err.Raise Err.Number, METHOD_NAME & ":" & Err.Source, Err.Description  
    End Sub Private Sub SetDataForSessionID() 
     On Error GoTo ErrHandler: 
     Const METHOD_NAME As String = "SetDataForSessionID" 
         Call mySessionPersistence.SaveSession(mySessionID,  
         myDSNString, myContentsEntity, myIsNewSession)  If myIsNewSession Then Call WriteSessionID(mySessionID)  Set myContentsEntity = Nothing 
     Set myObjectContext = Nothing 
     Set mySessionPersistence = Nothing 
     Exit Sub 
    ErrHandler: 
     Err.Raise Err.Number, METHOD_NAME & ":" & Err.Source, Err.Description 
    End Sub 
      
      

  5.   

    http://www.aspx8.com/teach/list.asp?id=498
      

  6.   

    这个办法好复杂,还要操作sql server,不过除此之外好象没什么别的方法了
      

  7.   

    还有就是用.net将web.config设置如下
    <sessionState mode="SQLServer"
                  cookieless="true|false"
                  timeout="number of minutes"
                  stateConnectionString="tcpip=server:port"
                  sqlConnectionString="sql connection string"
                  stateNetworkTimeout="number of seconds"
    />
    将session存入数据库。
      

  8.   

    看来将Session内容存入数据库中是一种很好的解决方案,除此之外好像确实没有别的什么办法,因为asp和asp.net的session语法就有区别。
      

  9.   

    自己UP
    微软没有为前后兼容问题提供解决方法吗?
    我只希望能用.NET读取ASP定义的Session变量,但不希望Session变量内容出现在HTML页面中被人看见!
    用SQL数据库到也行!但操作量大时会很影响速度吧!!
    真的是没更好和简单的方法了吗!有高手能帮忙的话就多谢谢了!~~
      

  10.   

    我前些天也遇到过这个问题,后来用一个很简单的方法给解决了,反正我要求的是实现了,不知能不能满足你的要求。
      a.asp中的sesion("code")传到b.aspx中
      在a.asp页面中有一个链接是链到b.aspx中的,<a href="b.aspx?code=Session("code")">传Session</a>
      在b.aspx中
            Dim code As String = Request.QueryString("code")
            Session("code") = code
     然后就可以了,你可以试试