Incorporating Existing Code






Incorporating Existing Code

You can continue to run existing ASP pages on your ASP.NET servers, convert the pages to the new format, or move COM components from ASP pages to ASP.NET pages. In this section, you learn about this level of interoperability.

Running ASP and ASP.NET Together

ASP and ASP.NET run well together on the same server—that's a fundamental consequence of the architecture of the two systems. Internet Information Services (IIS) forwards the requests for ASP pages to asp.dll (usually present in the System32\inetsrv folder of the Windows installation directory), whereas ASP.NET pages are handled by aspnet_isapi.dll (usually present in the Microsoft .NET Framework installation directory). Thus, there's no confusion on the part of the server between the two file types, and you don't need to worry that old pages will be executed incorrectly after you install ASP.NET.

graphics/alert_icon.gif

The Session state and the Application state are not shared between the ASP and ASP.NET pages. If you set a session or application variable in ASP.NET code, you can't retrieve it from ASP code, and vice versa.


Converting ASP Pages to ASP.NET

The syntax of ASP.NET pages is very close to the syntax of ASP pages, but it's not identical. Here's a partial list of things you might need to change if you want to convert an existing ASP page to run as an ASP.NET page:

  • In ASP, you can declare global variables and procedures in <%...%> blocks, and they will be visible to all code on the page. In ASP.NET, such variables and procedures should be declared inside a <script runat="server"> block. ASP.NET still executes code inside <%...%> blocks, but such code is executed at render time after all the code behind the page is already finished executing.

  • In ASP, you can mix programming languages within a single page. ASP.NET, however, requires each page to use a single programming language.

  • ASP uses scripting languages such as VBScript. ASP.NET uses object-oriented programming languages, such as C# and Visual Basic .NET.

  • ASP defaults to passing parameters by reference, and ASP.NET defaults to passing parameters by value.

  • The Set keyword, the Let keyword, and default properties have been removed from ASP.NET.

  • ASP allows you to use nondeclared variables. ASP.NET, on the other hand, requires you to declare all variables by default.

Using Late-bound COM Components

ASP.NET still supports the Server.CreateObject() method for creating late-bound COM components. For example, you can create an ADO Connection object in either an ASP page or an ASP.NET page with this line of code:

cnn = Server.CreateObject("ADODB.Connection");

Not all COM components can be instantiated in ASP.NET this way, however. In particular, components that use the Single-Threaded Apartment (STA) threading model do not function properly in ASP.NET pages unless you add a compatibility directive to the page, like so:

<[email protected] aspcompat="true"%>

You should note that C# is a strongly typed language and does not allow specific operations on generic objects whose type is unknown at compile time. A solution is to invoke the methods of the generic object dynamically by calling the Type.InvokeMember() method. The InvokeMember() method uses the following five parameters:

  • The name of the constructor, method, field, or property to invoke.

  • The type of the member to be invoked as specified by the System.Reflection.BindingFlags enumeration, such as InvokeMethod, SetProperty, GetProperty, and so on.

  • The binder object (a null value specifies that the default binder should be used).

  • The object on which this method needs to be invoked.

  • An array of objects to pass as the parameters to the method to be invoked. If no parameters are required to be passed, you can pass null. If the parameters are optional, you need to pass the Type.Missing field.

You should consider using late-bound COM objects as only a temporary measure. Although the ASP.NET engine will execute the new page, it won't benefit from any of the new capabilities of the .NET Framework.


     Python   SQL   Java   php   Perl 
     game development   web development   internet   *nix   graphics   hardware 
     telecommunications   C++ 
     Flash   Active Directory   Windows