热烈祝贺台州朗动科技的站长论坛隆重上线!(2012-05-28)    热烈庆祝伟大的祖国60周年生日 点击进来我们一起为她祝福吧(2009-09-26)    站长论坛禁止发布广告,一经发现立即删除。谢谢各位合作!.(2009-08-08)    热烈祝贺台州网址导航全面升级,全新版本上线!希望各位一如既往地支持台州网址导航的发展.(2009-03-28)    台州站长论坛恭祝各位新年快乐,牛年行大运!(2009-01-24)    台州Link正式更名为台州网址导航,专业做以台州网址为主的网址导航!(2008-05-23)    热烈祝贺台州Link资讯改名为中国站长资讯!希望在以后日子里得到大家的大力支持和帮助!(2008-04-10)    热烈祝贺台州Link论坛改名为台州站长论坛!希望大家继续支持和鼓励!(2008-04-10)    台州站长论坛原[社会琐碎]版块更名为[生活百科]版块!(2007-09-05)    特此通知:新台州站长论坛的数据信息全部升级成功!">特此通知:新台州站长论坛的数据信息全部升级成功!(2007-09-01)    台州站长论坛对未通过验证的会员进行合理的清除,请您谅解(2007-08-30)    台州网址导航|上网导航诚邀世界各地的网站友情链接和友谊联盟,共同引领网站导航、前进!(2007-08-30)    禁止发广告之类的帖,已发现立即删除!(2007-08-30)    希望各位上传与下载有用资源和最新信息(2007-08-30)    热烈祝贺台州站长论坛全面升级成功,全新上线!(2007-08-30)    
便民网址导航,轻松网上冲浪。
台州维博网络专业开发网站门户平台系统
您当前的位置: 首页 » ASP/ASP.NET编程 » ASP和ASP.Net共享Session解决办法

ASP和ASP.Net共享Session解决办法

论坛链接
  • ASP和ASP.Net共享Session解决办法
  • 发布时间:2007-09-16 22:19:28    浏览数:6719    发布者:lusm    设置字体【   
在原有的asp程序中用asp.net添加功能模块,共享SESSIONs是一个难点,下面介绍一种较为简洁的方法,可以在asp和asp.net中共享session会话。

登录页面使用C#重新创建,在成功登录后执行语句:

Response.Write("<script language='javascript'>window.open('ASPXTOASP.aspx','new');</script>");

打开aspxtoasp.aspx页面,action设为aspxtoasp.asp,即可将session值post到asp页面,因为是单向传递,不用在其他页面重复执行动作,经测试,传递成功!

Aspxtoasp.aspx代码如下:

<script language="C#" runat="server">
Response.Write( "<form name=t id=t action=ASPXTOASP.asp method=post>" );
foreach(object it in Session.Contents)
{
Response.Write("<input type=hidden name=" + it.ToString());
Response.Write( " value=" + Session[it.ToString()].ToString() + " >");
}
Response.Write("</FORM>");
Response.Write("<script>t.submit();</script>");
</script>

Aspxtoasp.asp代码如下:

<%
for i=1 to Request.Form.Count
Session(Request.Form.Key(i))=Request.Form(i)
next
Response.End
%>
<script language="JavaScript">
window.close();
</script>

这是一个老话题了,因为最近用的到,所以没办法又到处找资料。微软的网站上提供的是用数据库实现的,总觉得太麻烦,也有说直接使用的,但我没有试成功。我认为直接使用是不可能的。还有一种方法,就是通过几个页面转换,我使用的也是这个方法,通过总结搜索到的资料,整理后编写了几个页面来转换。主要是通过隐藏的input来实现的。具体方法如下:

asp 转 asp.net 页面:

用一个asp页,把session信息写到input中,提交给asp.net页

trans.asp

<%
’’----------测试数据--------
session("name")="srx"
session("id")="1"
session("sex")="f"
session("pass")="asdfas"
session("age")="23"
session("weight")="131"
’’--------------------------
Response.Write("<form name=frm id=frm action=""asptoaspx.aspx"" method=post >")
for each Item in Session.Contents
    Response.Write("<input type=hidden name=" & Item)
    Response.Write( " value=" & Session(item) & " >")
next
if len(Request.QueryString("Destpage")) >4 then  
    Response.Write("<input type=hidden name=DestPage value=" & Request.querystring("DestPage") & ">")
end if
Response.Write("</FORM>")
Response.Write("<scr" + "ipt>frm.submit();</scr" + "ipt>")
%>

asptoaspx.aspx

<%@ Page language="c#" %>
<script language=C# runat=server>
private void Page_Load(object sender, System.EventArgs e)
{
Session.Timeout = 60;
for(int i=0;i<Request.Form.Count;i++)
{
  Session[Request.Form.GetKey(i)]=Request.Form.ToString();
}
allsession(); //输出所有的Session,使用时可注释掉
try
{
if( Session["DestPage"].ToString().Length >4 )
{
 Server.Transfer(Session["DestPage"].ToString(),true);
}
}
catch {}
}
private void allsession()
{
Response.Write ("There are " + Session.Contents.Count +" Session <I>var</I>iables<P>");
foreach(object obj in Session.Contents)
{
 Response.Write("Session["+obj.ToString()+"] - "+Session[obj.ToString()].ToString()+"<br>");//输出所有的Session,使用时可注释掉
}
}
</script>

asp.net 转 asp 页面:

用一个asp.net页,把session信息写到input中,提交给asp页

trans.aspx

<%@ Page language="c#" %>
<script language=C# runat=server>
private void Page_Load(object sender, System.EventArgs e)
{
// ----------测试数据---------
Session["name"] = "srx";
Session["sex"]="F";
//----------------------------
Response.Write("<form name=frm id=frm action=aspxtoasp.asp method=post>");
foreach(object obj in Session.Contents)
{
 Response.Write("<input type=hidden name=’"+obj.ToString()+"’");
  Response.Write(" value = ’"+Session[obj.ToString()].ToString()+"’>");
}
try
{
if(Request.QueryString["DestPage"].ToString().Length > 4 )
{
 Response.Write("<input type=hidden name=’DestPage’");
 Response.Write(" value = ’"+Request.QueryString["DestPage"].ToString()+"’>");
}
}
catch{}
Response.Write("</form>");
Response.Write("<scr"+"ipt language=’javascript’>frm.submit();</scr"+"ipt>");
}
</script>

aspxtoasp.asp

<%
for i=1 to Request.Form.Count
Session(Request.Form.Key(i))=Request.Form(i)
next
if Len(Session("DestPage")) >4 then
  Response.Redirect(Session("DestPage"))
end if
’----------输出所有的Session-------------
call allsession() ’使用时注释掉此行代码即可
function allsession()
Response.Write "There are " & Session.Contents.Count &" Session <I>var</I>iables<P>"
Dim strName, iLoop
For Each strName in Session.Contents’使用For Each循环察看Session.Contents
 If IsArray(Session(strName)) then ’如果Session变量是一个数组? ’循环打印数组的每一个元素
  For iLoop = LBound(Session(strName)) to UBound(Session(strName))
    Response.Write strName & "(" & iLoop & ") - " & _
   Session(strName)(iLoop) & "<BR>"
   Next
 Else ’其他情况,就简单打印变量的值
  Response.Write strName & " - " & Session.Contents(strName) & "<BR>"
 End If
Next
end function
’--------------------------------------------
%>
娱乐休闲专区A 影视预告B 音乐咖啡C 英语阶梯D 生活百科
网页编程专区E AMPZF HTMLG CSSH JSI ASPJ PHPK JSPL MySQLM AJAX
Linux技术区 N 系统管理O 服务器架设P 网络/硬件Q 编程序开发R 内核/嵌入
管理中心专区S 发布网址T 版主议事U 事务处理