ASPAlliance.com : The #1 Active Server Pages .NET Community The #1 ASP.NET Community
Search   Search

Subscribe   Subscribe

Powered by ORCSWeb Hosting


Site Stats


Powered By ASP.NET
 
Featured Sponsor

Featured Columnist


Featured Book
Professional C# Web Services: Building .NET Web Services with ASP.NET and .NET Remoting
Professional C# Web Services: Building .NET Web Services with ASP.NET and .NET Remoting

Find Prices
Sample Chapter


New! asp.netPRO

We publish our articles in the standard RSS format.

Powerful .NET Email Component

Code Sharing Software

Migrate Classic ASP To ASP.NET

printable By Anjum Iqbal

Migration of your Classic ASP applications to ASP.NET can be a time consuming operation due to the fact that .NET application design/architecture is fundamentally different from Classic ASP, and in most cases it seems to be a complete rewrite of existing applications.

Classic ASP and ASP.NET are processed by their own respective engines, therefore to take advantage of new features and to levarge on the faster processing capabilities of ASP.NET, we can convert our Classic ASP pages to ASP.NET (Hybrid) pages. I call them Hybrid because in essence these pages are still ASP but will be processed by ASP.NET Engine.

Following list of 10 will help you achieve this with efficiently and without spending to much of your time.

1 ) Add a page level directive at the top of the .aspx page

<%@ Page Language="vb" aspcompat="true">

This directive tells .NET engine to expect use of VB.NET language in the page, it could very easily be "c#". The second part of this directive (aspcompat="true"), lets you use Older COM components which use Compartment Threading. This directive is optional, but as a quick reference, any of your ASP page which uses ADO is required to have this directive included.
 
2 ) In classic ASP, most popular way of reusing code is with #includes. They can stay as is in your .aspx pages. The alternate to that approach is to use User Controls. During my testing i found same performance results from both techniques. as an example if you have a common Header for all your .asp pages the include directive will look like this.

<!--#include virtual="/includes/header.asp"-->

To convert this to use User Controls, rename your Header.asp file to Header.ascx , and add

<%@ Control Language="vb" %>

at the top of that page, then in your .aspx page add following

<%@ Register TagPrefix="cc" TagName="Header" Src="/PerfTest/Header.ascx" %>

just after your Page directive and replace your #include statement with the following

<cc:Header id="Header1" runat="server" ></cc:Header>

 
3 ) .NET framework now requires you to declare all variables before you use them. so you have to Dim all your variables. It will be easier if your .ASP code was written with OPTION EXPLICIT
 
4 ) Keyword "Set" has been removed from vb language, so the staement

Set oCon=Server.CreateObject("ADODB.Connection")

will have to be re-written as,

oCon=Server.CreateObject("ADODB.Connection")

 
5 ) All your method calls now have to be in paranthesis () e.g.

Response.Write "Some Text to be written."

should be changed to

Response.Write ("Some Text to be written.")

 
6 ) The ASP Intrinsic objects namely Server, Request, Response, Application and Session are still maintained in .NET. You can use them without any change.
 
7 ) If you have Sub and Functions defined on the .ASP page using "<%" tag they have to be replaced with <SCRIPT LANGUAGE="VBScript" runat="server"> e.g.

<%
Function ReturnSomeValue(i)
    ReturnSomeValue= i+1
End Function
%>

Should be changed to

<SCRIPT LANGUAGE="VBScript" runat="server">
Function ReturnSomeValue(i)
    ReturnSomeValue= i+1
End Function
</SCRIPT>

 
8 ) The keyword IsNull or Null is no longer part of vb.NET. All comparisons and checks for "Null" values have to be replaced with System.DBNull.Value, e.g.

if ( isnull(n) ) then ...

Should be changed to

if (n is System.DBNull.Value ) then ...

 
9 ) .NET framework introduces a concept of Configuration file, namely web.config, which is an ideal place to store your application level constants like Database Connection strings, environment vars etc. In order to make use of this very convenient feature you first have to add web.config file to your Web Applications root folder. Add the AppSettings Section to this file. Web.Config file is a XML file, so it can be edited using any text editor.

<appSettings>< br>     <addkey="DBCon"value="server=localhost;Initial Catalog= Northwind;userid=sa;password=" />
</appSettings>

To read this setting in your .aspx page use,

System.Configuration.ConfigurationSettings.AppSettings("DBCon")

 
10 ) The absence of Framework provided Application Tracing made Classic ASP developers make use of Response.Write's. The .NET Framework now provides a very comprehensive tracing mechanism. to use that add Trace=True attribute the @Page directive of your page and then use

Trace.Write ("some trace message")

at any place you want. You dont have to delete or comment your Trace statements when you put your code to Production , simply remove the Trace=True attribute from @Page directive.

Following Code implements all above mentioned points for your reference

<!--##FILE = asp-convert.aspx## -->
<%@ Page Trace=False Language="vb" aspcompat="true" %>
<%@ Register TagPrefix="cc" TagName="Header" Src="Header.ascx" %>
<cc:Header id="Header1" runat="server" ></cc:Header>

<FORM name="Form1" runat=server>
<%
dim oCon

oCon=Server.CreateObject("ADODB.Connection") ' no SET keyword
oCon.ConnectionString = GetDBConnectionString ' test function Call
oCon.Open dim rs rs= Server.CreateObject("ADODB.Recordset")< BR> ' no SET keyword
Response.Write("<br>") ' use of () as in function calls
rs.Open ( "select * from Orders", oCon)

If Not (rs.BOF And rs.EOF) Then
  Do While Not rs.EOF
    if (rs("orderid").Value is System.DBNull.Value ) then
      ' null comparison
      Response.Write ("<Null>")
    else
      Response.Write (rs("orderid").Value)
      ' notice explicit use of .Value property
    End If
    Response.Write ("<br>")
    rs.MoveNext
  Loop
End If %>

<SCRIPT LANGUAGE="VBScript" runat="server">
Function GetDBConnectionString   
   GetDBConnectionString =    ConfigurationSettings.AppSettings("DBCon_BestAgent").ToString()
End function
</SCRIPT>
</form>
</body>
</html>

<!--##FILE = header.ascx## -->
<%@ Control Language="vb" %>
<html>
<body>
<h1>This is a Header</h1> <br> <br>

<!--##FILE = Web.Config## -->
<?xml version="1.0" encoding="utf-8" ?>
<Configuration>
  <system.web>
    <compilation defaultLanguage="vb" debug="false"/>
       <customErrors mode="Off" />
    <authentication mode= "Windows"></authentication>
  </system.web>
  <appSettings>
    <add key= "DBCon"value="server=localhost;Initial Catalog= Northwind;userid=sa;password=sa" />
  </appSettings>
</configuration>


Please send your comments and suggestions to Anjum Iqbal
 Copyright © 2000-2003 ASPAlliance.com  Page Rendered at 11/22/2009 1:06:38 PM