How to share Application and Session variables between Asp and Asp.net

Who I am
Joel Fulleda
@joelfulleda
Author and references

The coexistence between the ASP traditional and ASP.NET does not create problems on Microsoft servers since version 2000.

It is sufficient to enable ASP support and install the .NET Framework to be able to take advantage of both technologies in your web applications.

The problems arise in the need to use, exchange, read the Session and Application variables from both technologies.

How to share Application and Session variables between Asp and Asp.net



The variables of both technologies use memory areas within their execution environment and have nothing in common except the name, in fact the variables created by ASP will be readable only by this technology while those created by ASP.NET will be visible only within this environment. How can we fix this?

The trick, trivial but functional, is there: it is sufficient to request an ASP page from an ASP.NET page to read the contents of a variable Session e Application of that environment, and vice versa in order to read the objects present in the .NET pages from the ASP.

Let's try to read an Application variable present in the traditional ASP environment from an ASP.NET page. Let's start writing an ASP page containing this code by giving it the name "ReadAsp.asp":

<%
on error resume next

tipo=request(“type”)

name=request(“name”)

if tipo=”session” then

if Session(name)=”” then

Response.Write(“”)

else

Response.Write (Session(name))

endif

endif

if tipo=”application” then

ifApplication(name)=”” then

Response.Write(“”)

else

Response.Write(Application(name))

endif

endif

Response.End ()

%>

This code, which is also the one present in the example attached to this article, once called checks what type of variable you want to read and its name, then displays it with a trivial Response.Write. For example, to read and display the contents of the Application variable "example", it is sufficient to call this page with the desired parameters:



http://localhost/ReadAsp.asp?type=Application&name=esempio</a>

From our ASP.NET page it is sufficient to use the technique called “URL grabbing” to call that ASP page and fetch its content. If from our ASP.NET page we want to read the content of the "example" Application variable used in ASP, we can write:

HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(“http://localhost/ReadAsp.Asp?type=Application&name=esempio”);

HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();

StreamReader sr = new StreamReader(myResponse.GetResponseStream());

content_variable = sr.ReadToEnd ();

Simple, isn't it? To do the same thing but from an ASP page, we need to write an ASP.NET page named "ReadNet.aspx" with this content:


void Page_Load() {


string name=Request[“name”];

string tipo=Request[“type”];

if (tipo==”session”)

{

if (Session[name]==null) Response.Write (“”);

else

Response.Write (Session[name].ToString());

}

if (tipo==”application”)

{

if (Application[name]==null) Response.Write (“”);

else

Response.Write (Application[name].ToString());
}

}

So, to get the contents of an Application variable used in the .NET environment from our ASP page:


Dim objXMLHTTP, StrURL

StrURL = “http://localhost/ReadNet.aspx?type=application&name=esempio”

Set objXMLHTTP = Server.CreateObject(“MSXML2.ServerXMLHTTP”)

objXMLHTTP.Open “GET”, StrURL, false

StrCookie = Request.ServerVariables(“HTTP_COOKIE”) ‘ <- queste due righe verranno spiegate di seguito

objXMLHTTP.setRequestHeader “COOKIE”, StrCookie ‘ < …

objXMLHTTP.Send

contenuto_application=CStr(objXMLHTTP.ResponseText)

Set xml = Nothing

add a comment of How to share Application and Session variables between Asp and Asp.net
Comment sent successfully! We will review it in the next few hours.