Friday, May 21, 2010

How to migrate from ASP to ASP.NET

If you are using ASP, it is still not too late for you to migrate into latest ASP.NET. If you are worrying about the effort and the time for migration let me share with you my experience which may save you significant time and help you to migrate your project.

  • To start with you need to have Microsoft Visual Studio 2003. 
  • Install ASP to ASP.NET migration Assistant tool from http://www.asp.net/downloads/archived-v11/migration-assistants/asp-to-aspnet
  • After installing it, inside Visual Studio you will find a new option available under File -> Open -> Convert. Please go through this link http://www.asp101.com/articles/john/asptoaspnet/default.asp
  • Run this tool on your project completely, kindly note that your application will be converted using VB.NET language only. If you want to use C# language, you can find plenty of online converters. But it would be ideal to use VB.NET since it is syntactically almost same as ASP. 
  • Now let me tell you one of the major problem you may face. After converting your project completely to ASP.NET you would like to compile it to make it run and to your surprise you will end up getting hundreds of errors. Most of the errors would come due to your ASP style of coding in which  developers try to use many variable as global and share them across the asp pages. Since ASP is an interpreted language so if any used variable is available at runtime it won't yell. ASP.NET being a compiled language needs all variables or methods to be available at compile time.
  • To overcome the nightmares that came due to difference in interpreted vs compiled language processing, we used following methodology
    • All ASP pages which were included inside other ASP pages but were not used in showing UI. We renamed their extensions to .inc. This way ASP.NET compiler will ignore all pages with .inc extension and they will be included within your aspx page as if they were in ASP. Let me go into more details suppose you had a.asp & b.asp which were using a helper page c.asp. Now if a.asp is using some variable which is declared in c.asp or c.asp is calling a method which is declared in b.asp, after conversion and compilation you will get build error from both a.aspx and c.aspx. ASP.NET will try to compile each individual .aspx page and it will create a dynamic class and put all your code (inside script tag) within that and then try to compile. It will expect that your class (code inside script tag) has all dependency available which is essentially not there in our case. When we rename our c.aspx to c.aspx.inc and include it inside a.aspx and b.aspx. ASP.NET compiler will not compile c.aspx.inc separately but when it will go compiling a.aspx and b.aspx it will include c.aspx.inc code in that (as if they were part of those pages) and now a.aspx and b.aspx both will get compiled.
    • We made sure that there should not be any .inc file inside a .inc file. You have to put all dependency/include file of a UI page (aspx) within that page. This will prevent you the problems coming due to multilevel nesting of files and cyclic dependency. But it is all upto you, you may keep your ASP like structure as it is and yet make it run.
    • .inc file should not have any variable declared inside the file. We moved all the variables from .inc file to the page where .inc file is included. This way every page had its own contained local variables and there is no conflict with other pages. You may create another inc file which can include all variables used inside your any of inc and insert that file on every aspx page. This is again upto you how you want to implement it.
    • If a method is used in an inc file which is defined in another inc file but the second inc file was originally not included in your ASP page (now aspx) then just for the heck of preventing the compilation error don't include the "not required" inc file (basically try to maintain the same file include structure as you had in your ASP application). We initially started including the inc files which were not originally part of ASP page and ended up including almost all the inc files. To prevent compilation error, you need to create stub method/variable. for e.g. if a method Public Sub Test()  ... end Sub is causing this issue because it is implemented inside an inc file which was not included originally in your ASP page, then you just need to declare an object in you UI page as following Public Dim Test As Object. That means your aspx page (which is essentially a class) should have implementation of only those methods which are actually used inside it. If compiler is crying for some method which is actually not used inside your page but compiler needs it because of some dependency issue, you just create an empty implementation of that sub/function (instead of including a file where that method is implemented).
    • If the above mentioned method Test takes some arguments then you can provide a stub implementation of that as well for e.g. Public Sub Test (arg1 , arg2) 'no implementation End Sub. that is still better than including the complete file just the heck of stopping compilation error.
    • You may come across some object assignment issues. Please read this to know how to fix them http://aspnettechstuffs.blogspot.com/2010/05/how-to-assign-one-object-to-another-in.html
    • If you are using COM, you may need to fix few more things, read it  http://aspnettechstuffs.blogspot.com/2010/05/vbnet-to-interop-com-dll-call-failed.html
  • You will initially convert your project compatible to framework 1.1. Then after you have to follow a vertical development or horizontal whatever suits to your development cycle and slowly include all the best practices and coding guidelines. 
  • Since .net provides backward compatibility, you can use the same project in framework 2.0 and later slowly enhance it.
  • Read my small presentation to know the need of migration http://docs.google.com/present/edit?id=0AW5WT4_yjBDJZGdia3pqN2RfMWN0MzJybmY4&hl=en
If you get stuck somewhere, please write me a mail. I would try my best to resolve you query.
I strongly recommend to get out of ASP and use ASP.NET. you have to invest some time initially but later you will get immense benefits and a highly scalable application.


6 comments:

  1. Thank you very much for such knowledgeable and sincere help, this can come only from an expert.
    Your sincerity is greatly appreciated.

    ReplyDelete
  2. Hi, I am a newbie in ASP and / ASP.net. I want to know, is it possible to convert an ASP project to ASP.net using Visual Studios 2008 instead of 2003? I only have 2008 currently and I am doing research on how to do it - of course without having to write the code completely over again, and seem to not have seen any light as the articles that I found is using VS 2003. I tried exploring my VS 2008, but I did not see any convert options.
    Thank you very much.

    ReplyDelete
  3. Hi Lim, thanks for visiting my site.

    You can run same migration assistant from command line as well
    http://www.asp.net/downloads/archived-v11/migration-assistants/asp-to-aspnet/overview

    You may also look at http://www.artinsoft.com/so_asp.aspx
    but mindful of artinsoft license cost.

    ReplyDelete
  4. Hi Abhishek,

    Thanks for your prompt reply..
    I have tried the first one, but to no avail. Perhaps I got the code wrongly.

    I tried the command that were 'instructed', changed the , tried to put ' " " ' in between the path where there's a space, but still wrong. Could you be kind so as to help me out? Perhaps I was missing a step?

    And, the code below is what we have to write in the command right?

    I really appreciate your help. :)

    Have a good day.

    ASPUpgrade[.exe] [/Out ] [/NoLog | /LogFile ] [/Verbose] [ /ProjectName ] [/ForceOverwrite]

    ReplyDelete
  5. Great tips for asp to asp.net migration.

    ReplyDelete