Asp. Net migration notes

Source: Internet
Author: User
Tags date array continue integer new features numeric value variables require
asp.net ASP. Net migration notes

Unified Teaching Network
Date: 2001-11-5 9:05:00
Source: ZDNet.com.cn
Author: unknown
While Microsoft ASP.net's designers are doing a great job of backwards compatibility with ASP applications, there are a few things to pay special attention to when trying to move Web applications from ASP to asp.net. Practical understanding by. NET platform changes or introduced technology and ASP.net, will make the porting process relatively simple.

To enhance your understanding of how to make an ASP application work smoothly in an ASP. NET environment, this article explores many aspects of technological change. It is also pointed out that some asp.net can be used to improve the new functionality of existing applications. This is not a comprehensive understanding of asp.net new features of the article. It's primarily about what you need to know to implement a successful transplant.

Since most ASP applications use Microsoft Visual Basic®scripting Edition (VBScript), I estimate that most of you will choose to use Visual Basic. NET porting to asp.net. This is clearly not a necessary requirement, but changing the programming language while deciding on a transplant will require some extra effort and may well require design changes.

Coexistence
Before we begin to discuss specific compatibility and porting issues, you must understand how ASPs and asp.net coexist. ASP and asp.net applications can run together on the server without rejecting each other. This is mainly because the file suffix (. asp versus. aspx) used by both technologies differs from the configuration mode (Metabase/registry versus xml-based configuration files). The two systems have distinct processing engines.

It is entirely possible to run an ASP on a portion of an application, while another part of it runs ASP.net. This is important if you need to gradually change from a site that is both huge and very fast to asp.net. One might think it would be better to pack the whole site at once. This may be true for some types of Web applications, but I think many existing websites do not apply because of their scale, complexity and rapid innovation in the content and form of the Web site. After all, if your site is profitable, it's likely that the investors will continue to let you add new features, leaving you unable to turn to this new hot technology. In addition, if you plan to turn to ASP.net and make it a long-term investment, you will want to use this opportunity to improve the structure and design as much as possible. In these cases, it is necessary to achieve coexistence in a gradual manner.

Compatibility issues
Porting your application to asp.net may not be easy, but it's not that hard. Asp. NET compatibility with ASP is very good. It's not easy to do this because it's a whole new change from ASP to asp.net. Asp. NET designers originally planned to do 100% and ASP compatible, but ultimately for the long-term consideration, for the purpose of optimizing the platform, had to withdraw the plan. You don't have to worry that all the changes are going in the right direction and won't cause a lot of work when you install. The combination of actual changes can be divided into the following:
 

Core API Changes
Structural changes
Visual Basic Language changes
COM-related changes
Changes to application Configuration
State management issues
Security issues involved
Data access

All of the above will be discussed in detail below.


Core API Changes

The core API for ASPs consists of a number of intrinsic objects (Request, Response, Server, etc.) and the methods associated with them. These APIs continue to work correctly under ASP.net, except for some simple changes. All changes are related to the request object. Please look at table one:

Table 1:api Changes

Request (item)
In ASP, this method returns an array of strings. in ASP. NET, it returns NameValueCollection.
Request.QueryString (item)
In ASP, this method returns an array of strings. in ASP. NET, it returns NameValueCollection.
Request.Form (item)
In ASP, this method returns an array of strings. in ASP. NET, it returns NameValueCollection.


As you can see, these changes are the same for all methods.

If the object you are accessing has a unique value for a key value, you do not need to modify your code. However, if you have multiple values, you need to get them in a different way. Also, note the visual Basic. NET is based on zeros, and the set of VBScript is one-base.

For example, in an ASP, accessing a single query character channeling value from a request made to http://localhost/myweb/valuetest.asp?values=10&values=20 may result in the following results:

<%
' This would output ' 10 '
Response.Write Request.QueryString ("Values") (1)

' This would output ' 20 '
Response.Write Request.QueryString ("Values") (2)
%>


in ASP. NET, the QueryString property returns the NameValueCollection object. From this object, you can get a collection of values and the real content you want. Again, note that the first item in the collection is based on zero instead of one:

<%
' This would output ' 10 '
Response.Write (Request.QueryString.GetValues ("Values") (0))

' This would output ' 20 '
Response.Write (Request.QueryString.GetValues ("Values") (1))
%>

Whether ASP or asp.net, the following code execution results are the same:

<%
' This would output ' 10 ', ' 20 '
Response.Write (Request.QueryString ("values"))
%>
 
Structural change refers to changes that affect the layout and code style of active Server pages. You must have an understanding of them to make sure that your code runs correctly in asp.net.


Code blocks: declaring functions and variables

In ASP, you can declare subroutines and global variables in your code designator.

<%
Dim X
Dim Str
Sub mysub ()
Response.Write "This is a string."
End Sub
%>
In ASP.net, this has been banned. You must declare all functions and variables in the <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>


Programming language Blending

In the ASP programming language, you basically have two choices: VBScript or microsoft®jscript®. You can mix and match script blocks freely on the same page.

At ASP.net, you now have three choices. You can use C #, Visual Basic. NET, or Jscript. Notice that I'm talking about visual Basic. NET rather than VBScript. This is because VBScript does not exist on the. NET platform. It has been completely visual Basic. NET into. Although you are free to choose any language, it is important to note that you can no longer mix different languages on the same page. Of course, you can include C # code in your application page1.aspx, and Page2.aspx contains visual Basic. NET code. What you can't do is mix them on the same page.


New Page Directives

In ASP, you must place all instructions on the first line of the page and only within the same code marker. For example:
<%language= "VBSCRIPT" codepage= "932"%>
In asp.net, you must put the language instruction and the page instruction on one line. For example:
<% @Page language= "VB" codepage= "932"%>
<% @QutputCache duration= "varybyparam=" "None"%>
You can add as many instructions as you want. Directives can be placed anywhere in your. apsx file, but the standard practice is to place the file at the beginning.

Asp. NET incorporates a number of new directives. I recommend that you look for more information in the ASP.net related files to see if the new instructions will benefit your application.


The Render function is no longer valid
In ASP, developers find that they can use the so-called "output function" to do something smart. An output function is a subroutine with a large amount of HTML embedded in the body. For example:
<%sub Renderme ()
%>
<H3> This is HTML text being rendered. </H3>
<%end Sub
Renderme
%>
Although you can do a lot of cool things with these functions, the code is no longer allowed in asp.net, which should be a step forward. I believe that when you start mixing or pairing code with HTML like this, you've seen some functions that are quickly unreadable and manageable. The easiest way to get it to work in ASP.net is to call Response.Write to replace your HTML output. For example:

<script language= "VB" runat= "Server" >
Sub Renderme ()
Response.Write ("<H3>" is HTML text being rendered.) </H3> ")
End Sub
</script>

<%
Call Renderme ()
%>

Notice what I said: "The simplest way", which is not to say that it is the best way. Depending on the complexity and quantity of your output code, it may be in your interest to use a custom Web controls. It allows you to set HTML properties with programs and really separate your content from the code. This can greatly improve the readability of your code.
 
As I mentioned, VBScript has been more comprehensive and powerful in visual Basic. NET replaced. In this section, I will highlight those issues that you might encounter that are related to the changes in Visual Basic language. Note that I do not list all of the changes in Visual Basic, but only focus on a Visual Basic. NET toward ASP. NET Asp/vbscript programmers might encounter. For all language changes, please refer to Visual Basic. NET related documents.


Farewell Variant data type

We know it, we love it, we hate it. I mean, of course, variant data types. The Variant data type is not part of. NET, so in visual Basic. NET also does not support it. This means that all of your ASP variables are unknowingly converting from variant data types to object types. Most variables in your application should be changed to the corresponding primitive type as needed. If your variable is indeed an object type in Visual Basic terminology, simply declare it as object in asp.net.


Visual Basic Data types

A VARIANT that is bound to cause particular concern is the Vt_date data type. It is represented as the date type in Visual Basic. In Visual Basic, Date is stored in a 4-byte double-byte format. In Visual Basic.NET, date uses the common Language Runtime DateTime type, which is stored with a 8-byte integer.

Since all variables are variant data types in ASP, the date variables that you use are compiled and may continue to work (depending on how they are used correctly). However, when you use these variables, you may also encounter a variety of problems because their actual data types have changed. It is especially noteworthy when you send a numeric value as a long integer type into a COM object, or when you perform some conversion operations on a date type with CLNG.


Option Explicit is now the default

In ASP, Option Explicit is not defined as the default setting. Visual Basic. NET changes at this point. Option explicit becomes the default value, so all variables need to be declared. If you are more strict, you can change your settings to option Strict, which forces you to declare all variables to be of a certain data type. Although this seems redundant, it is a practice you should stick to. If you do not, your code is far from optimized because all undeclared variables become object types. Most implicit conversions still work, but if you declare all the variables, you can be more comfortable later.


Let and set are no longer supported

objects can be directly assigned like this: MyObj1 = MyObj2. If you have a set or let keyword in your program, they must be removed.


Implement method calls with parentheses

In ASP, you are free to invoke the object's methods without using parentheses. For example:

Sub WriteData ()
Response.Write "This Is data"
End Sub
WriteData
in ASP. NET, you must use parentheses in any invocation, even if you do not use any parameters. Write the code as shown in the following example, so that the code can be used in ASP and ASP. NET environment, all are working correctly.

Sub WriteData ()
Response.Write ("This Is Data")
End Sub
Call WriteData ()


ByVal becomes The default

In Visual Basic, all parameters are passed by default in reference (or ByRef) mode. In Visual Basic. NET changes. All parameters are passed by default with a value (or ByVal). If you still want to pass as a reference, you must clearly indicate the ByRef keyword before the parameter. For example:

Sub mybyrefsub (ByRef Value)
Value = 53;
End Sub
This is where you have to be very careful. When you turn your code to ASP.net, I recommend that you check every parameter in the method call two or even three times to make sure that the change does meet your expectations. I estimate that there will always be some mistakes in the inspection process.


No longer have default properties

The concept of default properties no longer exists in visual Basic. NET. This means that if you have an ASP code that relies on the default attributes from an object, you will need to modify it to explicitly reference the desired property. Please see the following example:



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.