Optimizing the ASP.NET Application


The Migration Assistant tool converts ASP code to ASP.NET code without optimizing it. You need to manually segregate the business logic and the presentation logic, create classes, use ADO.NET objects, and rename the methods. The Complaint Monitoring System application's ASP files are converted to ASP.NET files. You need to create the .vb files that separate the business logic from the presentation logic. You may need to perform various steps to optimize the ASP.NET code, such as define the scope of variables, extend objects, rename methods, access .NET resources, and implement security.

Note

The following sections explain the steps to optimize the ASP.NET code for the loginhandle.vb file of the Complaint Monitoring System. You need to implement similar changes in all the files of the application.

Defining the Scope of Variables

Each Web page in ASP.NET is a class that inherits the System.Web.UI.Page class. You need to define all the variables on an ASP page as member variables of the class that represents the corresponding ASP.NET page. For example, you create a loginhandle.vb file that defines the loginhandle class. This class provides the business logic for the loginhandle.aspx file. In the loginhandle.vb file, you define all the variables as member variables of the loginhandle class.

Listing 3-21 shows the loginhandle class that contains all the variables as member variables:

Listing 3-21: Member Variables of the Loginhandle Class

start example
 Public Class loginhandle Inherits System.Web.UI.Page Dim mysql As String 'Remove the declaration of rs from the code 'Dim rs As New ADODB.Recordset 'Add a dataset,SqlCommand and SqlDataAdapter object in the code Dim ds As New DataSet Dim cmd As SqlCommand Dim da As SqlDataAdapter Dim username As String Dim password As String   
end example

The above listing shows the member variables of the loginhandle class. You need to define all the variables on an ASP page as member variables of the class that represents the corresponding ASP.NET page.

Extending Objects

ASP does not support inheritance and overloading, while ASP.NET support these features. To optimize converted code, you can use the built-in classes of .NET Framework. For example, in the loginhandle.vb file, you create a loginhandle class that inherits System.Web.UI.Page.

The following code shows how to create a loginhandle class that inherits System.Web.UI.Page:

 Public Class loginhandle     Inherits System.Web.UI.Page   

The loginhandle.aspx file then includes the loginhandle class that the loginhandle.vb file provides to use the functionality of the loginhandle class, as shown:

 <%@ Page language="VB" inherits="loginhandle" src="/books/2/923/1/html/2/loginhandle.vb" %>  

Similarly, in the CheckStatusHandle.vb file, you inherit System.Web.UI.Page, as shown:

 Public Class CheckStatusHandle     Inherits System.Web.UI.Page 

The checkstatushandle.aspx file includes the CheckStatusHandle class defined in the CheckStatusHandle.vb file to use the functionality of the ChechStatusHandle class, as shown:

 <%@ Page language="VB" inherits="CheckStatusHandle" scr="CheckStatusHandle.vb"%> 

Renaming Methods

The names of the methods created in the ASP application may conflict with the built-in methods of .NET Framework. You need to change the names of the methods for them to function in the .NET environment. For example, in the complaint.asp file, you need to change the validate() method because the Page class in .NET Framework contains the validate method that does not match the signature of the validate() method on the complaint.asp page.

Listing 3-22 shows the renamed ValidateInformation() method:

Listing 3-22: The ValidateInformation() Method

start example
 'This method validates the data on the form 'Public Function validate(ByVal form1) 'End Function 'Rename the method name from validate to ValidateInformation  Public Function ValidateInformation() As Boolean If txtName.Text = "" Then lblError.Visible = True lblError.Text = "Please enter your name" Return False End If If txtOthers.Text = "" Then lblError.Visible = True lblError.Text = "Please enter your problem" Return False End If If txtMaccode.Text = "" Then lblError.Visible = True lblError.Text = "Please enter the machine code" Return False End If If txtPerCode.Text = "" Then lblError.Visible = True lblError.Text = "Please enter the peripheral code" Return False End If Return True End Function  
end example

The above listing shows the ValidateInformation() method, which was originally the Validate() method.

Accessing .NET Resources

.NET provides resources that allow you to perform different functions, such as state management and database handling. You need to import namespaces, such as System.Web and System.Data, into your application to utilize .NET resources and their functions in your applications.

Listing 3-23 shows the loginhandle.vb file that imports different namespaces to utilize the functions of the classes and interfaces the namespaces provide:

Listing 3-23: Importing Namespaces

start example
 Imports System.Web 'Allows browser-server communication and includes classes, such as HttpRequest and HttpResponse.  Imports System.Web.SessionState 'Allows storage of data specific to a single client inside a Web application on the server and includes classes, such as HttpSessionState and SessionStateModule.  Imports System.Web.UI 'Allows you to create and use ASP.NET server controls and pages that appears in the Web application as user interface elements and includes classes, such as Control class and Page class. Imports System.Data 'Contains classes that forms the ADO.NET architecture. Imports System.Data.SqlClient  'Accesses the SQL Server database in the managed environment. It is the .NET Framework Data Provider for SQL server. 
end example

The above listing shows the different namespaces that the loginhandle.vb file imports.

Listing 3-24 shows the loginhandle.vb file that shows how to utilize different namespaces in the loginhandle class:

Listing 3-24: Utilizing Namespaces in the loginhandle.vb File

start example
 Public Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) username = Request.Form.Item("txtlogin") password = Request.Form.Item("txtpassword") mysql = "select * from Users where UserId='" &username & "'and password='" &  password & "'" cmd = New SqlCommand(mysql, Application("Connection")) da = New SqlDataAdapter(cmd) da.Fill(ds) If Not ds.Tables.Count = 0 And Not ds.Tables(0).Rows.Count = 0 Then If username = "administrator" Then Session("Logintest") = Request.Form.Item("txtlogin") Session("Passwordtest") = Request.Form.Item("txtpassword") Response.Redirect("admin.aspx") Else Session("Logintest") =   Request.Form.Item("txtlogin") Session("Passwordtest") = Request.Form.Item("txtpassword") Response.Redirect("complaint.aspx") End If Else Session("Logintest") = "" Session("Passwordtest") = "" Response.Write(("<h2><center> <font color='#FFFFFF'><br><br><br><br><br>Invalid Username or Password</font> </center></h2>")) Response.Write(("<h2><center> <font color='#FFFFFF'>Please try again!!!</font> </center></h2>")) End If End Sub 
end example

The above listing shows the Page_Load() method that utilizes the different namespaces included in the loginhandle.vb file, such as System.Web, System.Web.UI, and System.Data.

Implementing Security

ASP applications do not include a Web.config file so you have to configure the security settings through IIS. .NET automatically creates a Web.config file when you create a new ASP.NET project. You need to configure the security settings of the Web.config file per the security requirements of your application. For example, the Complaint Monitoring System application denies access to anonymous users.

Listing 3-25 shows the Web.config file to implement security for the Complaint Monitoring System application:

Listing 3-25: The Complaint Monitoring System Application's Web.config File

start example
 <?xml version="1.0" encoding="utf-8"?> <configuration> <system.web> <authentication mode="Windows" />  <!--  AUTHORIZATION  This section sets the authorization policies of the application. You can allow or deny access to application resources by user or role. Wildcards: "*" mean everyone, "?" means anonymous  (unauthenticated) users. --> <authorization> <deny users="?" /> <!-- deny anonymous users --> </authorization>   </system.web> </configuration> 
end example

In the above listing, the authentication and authorization element of the Web.config file is configured for security. You have to configure the elements to deny anonymous users access to the Complaint Monitoring System application.




Migrating Unmanaged Applications to. NET
Migrating Unmanaged Applications to. NET
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 31

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net