由于以前系统一直用的ASP开发的代码,现在想转向ASP.NET,而以前的代码不可能都重写,造成了ASP和ASP.NET共存的现象.
我发现ASP.NET要读取ASP中的session是件很麻烦的事情,各位大哥有没有什么好的或者通用的解决方法,谢谢了.
此外,我还想问一下,系统中同时存在ASP和ASP.NET两种代码还有没有别的什么问题,请各位高手指点一下.
说明一下,小弟是初学者,以前是做嵌入式开发的,转行做这个才1个月时间,大家多多关照哦,谢谢.

解决方案 »

  1.   

    如果在两个站点中可以在ASP中POST到ASP.NET中再写一次Session。如果在同一个站点就不用了。
    可以同时存在ASP和ASP.NET的。
      

  2.   

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

  3.   

    ASP和asp.net中SESSION不能共享.你也可以试试COOKIE.好像ASP中写的COOKIE和ASPNET中写的不一样.你可以查些资料
    我碰到时是用第一种传参的方法了.就是从asp到aspx或 aspx到asp都经过处理传参你可以试试这个用隐藏表单的方式来将asp.net中的session传入asp中:
    http://www.eggheadcafe.com/articles/20021207.asp另外还有一些其它方法:
    http://www.eggheadcafe.com/articles/20041017.asp另外.MSDN上有一种方法 :
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/converttoaspnet.asp
      

  4.   

    asp和asp.net虽然能够一起工作,但他们是各干各的互不影响,所以他们的SESSION是不能共享的。
      

  5.   

    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); 

        SessionPersistence类使用微软.NET框架组件的BinaryFormatter来串行化和并行化对话状态为二进制格式以提供最佳性能。结果的二进制对话数据接着作为图象字段类型被存入SQL Server。 
    public mySession LoadSession(string key, string dsn,  
    int SessionExpiration) 

     SqlConnection conn = new SqlConnection(dsn); 
     SqlCommand LoadCmd = new SqlCommand(); 
     LoadCmd.CommandText = command; 
     LoadCmd.Connection = conn; 
     SqlDataReader reader = null; 
     mySession Session = null;  try 
     { 
      LoadCmd.Parameters.Add("@ID", new Guid(key)); 
      conn.Open(); 
      reader = LoadCmd.ExecuteReader(); 
      if (reader.Read()) 
      { 
       DateTime LastAccessed =reader.GetDateTime(1).AddMinutes(SessionExpiration); 
       if (LastAccessed >= DateTime.Now) 
        Session = Deserialize((Byte[])reader["Data"]); 
      } 
     } 
     finally 
     { 
      if (reader != null) 
       reader.Close(); 
      if (conn != null) 
       conn.Close(); 
     }  return Session; 

    private mySession Deserialize(Byte[] state) 

     if (state == null) return null;  mySession Session = null; 
     Stream stream = null;  try 
     { 
      stream = new MemoryStream(); 
      stream.Write(state, 0, state.Length); 
      stream.Position = 0; 
      IFormatter formatter = new BinaryFormatter(); 
      Session = (mySession)formatter.Deserialize(stream); 
     } 
     finally 
     { 
      if (stream != null) 
       stream.Close(); 
     } 
     return Session; 

        在请求的末尾,Page类的Unload事件被启动了,一个同Unload事件一起注册的事件处理方法将串行化对话数据为二进制格式并将结果二进制数据存入SQL Server。