migrating to asp .net: key considerations
jim kieley
microsoft corporation
july 2001
summary: this article explores some basic considerations for moving existing asp applications to the asp .net environment as quickly and efficiently as possible. (18 printed pages)contents
introduction
compatibility issues
core api changes
structural changes
visual basic language changes
com-related changes
application configuration changes
state management
security-related changes
data access
preparation for asp .net
summary introduction
although the designers of microsoft® asp .net have done an excellent job in preserving backward compatibility with asp applications, there are a few key items you need to be aware of before undertaking the effort of moving a web application from asp to asp .net. a solid understanding of the technologies that have changed or been introduced with the .net platform and asp .net will go a long way in making this process a whole lot easier.
this article explores a number of areas of change to give a clear understanding of the efforts involved in getting an asp application up and running in the asp .net environment. at the same time, it points out some of the new features of asp .net that can be leveraged to improve an existing application. this is by no means a comprehensive look at all of the new features in asp .net. instead, it focuses on areas that you need to know now for successful migration.
i am assuming that since the majority of asp applications use microsoft® visual basic® scripting edition (vbscript), most of you will elect to migrate to asp .net using visual basic .net. this is obviously not a requirement, but changing languages at the same time you decide to migrate will take some additional effort and most likely will include design and architectural changes as well.coexistence
before we get into discussing specific compatibility and migration issues, it is important that you understand how asp and asp .net can coexist. both asp and asp .net applications can run side by side on a server without adversely affecting each other. this is primarily due to the fact that separate file extensions (.asp versus .aspx) and separate configuration models (metabase/registry versus xml-based configuration files) are used between the two technologies. the two systems have totally separate processing engines.
it is entirely possible to have part of one application running asp and another part of the same application running asp .net. this is very beneficial if you need to move a large, rapidly changing site to asp .net one piece at a time. some would argue that you may be better off porting and deploying the entire site all at once. this may be the case for certain classes of web applications, but i think that there are a lot of sites out there where this may not be feasible due to the sheer size, complexity, and rapid evolution of the site's content and presentation. after all, if you are sitting on a profitable web site, chances are the people paying the bills will not allow you to stop implementing their new features so that you can move things over to this hot new technology. additionally, if you are going to put forth the effort to move to asp .net as a long-term investment, you will want to use this chance to make as many architectural and design improvements as you can. for these types of situations, coexistence using a phased-in approach is an absolute must.compatibility issues
migrating your application to asp .net may not be easy; however, it should not be that difficult either. asp .net is very much compatible with asp. this is impressive given the fact that asp .net is a complete overhaul of asp. the designers of asp .net had an initial goal of being 100 percent backwards compatible with asp but subsequently had to back off this goal in favor of improving the platform for the long haul. not to worry—the changes made were for the better and should not require a lot of work on your part to implement. the actual changes made can be categorized into the following sections: - core api changes
- structural changes
- visual basic language changes
- com-related changes
- application configuration changes
- state management issues
- security-related changes
- data access
each of these areas will be discussed in detail.core api changes
the core apis of asp consist of a few intrinsic objects (request, response, server, etc.) and their associated methods. with the exception of a few simple changes, these apis continue to function correctly under asp .net. all of the changes are related to the request object and are shown in table 1:
table 1. api changesmethodchangerequest(item)in asp, this method will return an array of strings. in asp .net, it returns a namevaluecollection.request.querystring(item)in asp, this method will return an array of strings. in asp .net, it returns a namevaluecollection.request.form(item)in asp, this method will return an array of strings. in asp .net, it returns a namevaluecollection.
as you can see, the changes are basically the same for all methods involved.
if the item you are accessing contains exactly one value for the specified key, you do not need to modify your code. however, if there are multiple values for a given key, you need to use a different method to return the collection of values. also, note that collections in visual basic .net are zero-based, whereas the collections in vbscript are one-based.
for example, in asp the individual query string values from a request to http://localhost/myweb/valuetest.asp?values=10&values=20 would be accessed as follows:<% 'this will output "10" response.write request.querystring("values")(1) 'this will output "20" response.write request.querystring("values")(2)%>
in asp .net, the querystring property returns a namevaluecollection object from which you need to retrieve the values collection before retrieving the actual item you want. again, note the first item in the collection is retrieved by using an index of zero rather than one: <% 'this will output "10" response.write (request.querystring.getvalues("values")(0)) 'this will output "20" response.write (request.querystring.getvalues("values")(1))%>
in both the case of asp and asp .net, the follow code will behave identically:<% 'this will output "10", "20" response.write (request.querystring("values"))%>
structural changes
structural changes are those that affect the layout and coding style of active server pages. you need to be aware of several of these to ensure your code will work in asp .net.code blocks: declaring functions and variables
in asp, you can declare subroutines and global variables in between your code delimiters. <% dim x dim str sub mysub() response.write "this is a string." end sub %>
in asp .net, this is no longer allowed. you must instead declare all of your functions and variables inside a <script> block.<script language = "vb" runat = "server"> dim str as string dim x, y as integer function add(i as integer, j as integer) as integer return (i + j) end function</script>
mixing programming languages
in asp, you basically have two choices for your programming language: vbscript or microsoft® jscript®. you are free to mix and match blocks of script in the same page at will.
in asp .net, you currently have three options. you can use c#, visual basic .net, or jscript. note that i said visual basic .net instead of vbscript. this is because vbscript does not exist in the .net platform. it has been fully subsumed by visual basic .net. although you are free to pick any of these languages, it is important to note that you cannot mix languages on the same page as you could do in asp. it is certainly possible to have page1.aspx of your application contain c# code while page2.aspx of the same application contains visual basic .net code. you just cannot mix them together in a single page. new page directives
in asp you must place all directives on the first line of a page within the same delimiting block. for example:<%language="vbscript" codepage="932"%>
in asp .net, you are now required to place the language directive with a page directive, as follows:<%@page language="vb" codepage="932"%><%@qutputcache duration="60" varybyparam="none" %>
you can have as many lines of directives as you need. directives may be located anywhere in your .apsx file but standard practice is to place them at the beginning of the file.
several new directives have been added in asp .net. i encourage you to look these up in the asp .net documentation to see how they may benefit your application.render functions are no longer valid
in asp, developers figured out that they could do clever things by using what is termed a "render function." a render function is basically a subroutine that contains chunks of html embedded throughout its body. for example:<%sub renderme()%><h3> this is html text being rendered. </h3><%end subrenderme%>
although you can do some cool things using these types of functions, this type of coding is no longer allowed in asp .net. this is probably for the better. i am sure you have seen functions that quickly become unreadable and unmanageable when you start to mix and match code and html like this. the simplest way to make this work in asp .net is to replace your html outputs with calls to response.write as follows:<script language="vb" runat="server"> sub renderme() response.write("<h3> this is html text being rendered. </h3>") end sub</script><% call renderme()%>
note that i said "simplest way." this does not necessarily mean it is the best way. depending on the complexity and amount of your rendering code, it may be beneficial for you to look into using custom web controls, which allow you to programmatically set your html attributes and truly separate your code from your content. doing so makes for much more readable code.visual basic language changes
as i mentioned earlier, vbscript has been deprecated in favor of the more complete and more powerful visual basic .net. in this section, i will highlight some of the issues you are likely to encounter related to visual basic language changes. it is important to note that this is not meant to be an exhaustive list of all of the changes made to visual basic. instead, i have focused on the items that you as an asp/vbscript programmer will likely encounter moving to asp .net using visual basic .net. consult the visual basic .net documentation for a complete list of all language changes that have been made. farewell to the variant data type
we know it, we love it, we love to hate it. i am speaking of a variant data type, of course. variants are not a part of .net and thus are not supported in visual basic .net. what this means is that all of your asp variables are silently going to move from variant types to object types. most variables used in your application can and should be changed to a corresponding primitive type depending on your needs. if your variable is really an object type in visual basic terms, simply explicitly declare it as an object type in asp .net. visual basic date type
one variant type that warrants some special attention is the vt_date type, which manifests itself in visual basic as a date type. in visual basic, a date is stored in a double format using four bytes. in visual basic .net, date uses the common language runtime datetime type, which has an eight byte integer representation.
since everything is a variant in asp, your intended date variables will compile and may continue to work depending on how they are used. it is possible, however, that you will run into some unexpected problems performing certain operations with the variable because the underlying type has been changed. pay attention to areas where you may be passing the date value into com objects as long integer values or performing certain casting operations on date types using clng.option explicit is now the default
in asp, the option explicit keywords were available but were not enforced as the default. in visual basic .net, this has changed. option explicit is now the default so all variables need to be declared. it is good practice to be even more rigid than this and change your setting to option strict. doing so forces you to declare all of your variables as a specific data type. while this may seem like extra work, it really is the way you should be doing things anyway. if you choose not to, your code will be less than optimal as all undeclared variables will become object types. most implicit conversions will still work, but you will probably be better off and safer if you explicitly declare all of your variables to the types you want them to be.let and set are no longer supported
objects can be assigned to one another directly like this: myobj1 = myobj2. you no longer need to use the set or let statements. if you use these statements, they must be removed.