Migrating ASP to ASP.NET
Total Page:16
File Type:pdf, Size:1020Kb
APPENDIX Migrating ASP to ASP.NET IN THIS APPENDIX, we will demonstrate how to migrate an existing ASP web site to ASP.NET, and we will discuss a few of the important choices that you must make during the process. You can also refer to the MSDN documentation at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/ html/cpconmigratingasppagestoasp.asp for more information. If you work in a ColdFusion environment, see http: //msdn. microsoft. com/library I default.asp?url=/library/en-us/dnaspp/html/coldfusiontoaspnet.asp. ASP.NET Improvements The business benefits of creating a web application in ASP.NET include the following: • Speed: Better caching and cache fusion in web farms make ASP.NET 3-5 times faster than ASP. • CompUed execution: No explicit compile step is required to update compo nents. ASP.NET automatically detects changes, compiles the files if needed, and readies the compiled results, without the need to restart the server. • Flexible caching: Individual parts of a page, its code, and its data can be cached separately. This improves performance dramatically because repeated requests for data-driven pages no longer require you to query the database on every request. • Web farm session state: ASP.NET session state allows session data to be shared across all machines in a web farm, which enables faster and more efficient caching. • Protection: ASP.NET automatically detects and recovers from errors such as deadlocks and memory leaks. If an old process is tying up a significant amount of resources, ASP.NET can start a new version of the same process and dispose of the old one. 373 Appendix The programming benefits of creating a web application in ASP.NET include the following: • Programming models: The Web Forms programming model is a browser independent user interface that processes data on the web server so you do not have to create browser-specific versions. The XML web services programming model uses components that run on the server, typically include business logic, and are available via universal web protocols. (We do not cover XML web services in this appendix.) Server controls: This HTML-like style of declarative programming enables applications to be built with far less code than with classic ASP. This allows programmers to focus on the logic of page execution rather than on the HTML coding details and particularities of each browser. • Flexible language options: You can work in any of over 25 .NET languages, including VB, C++, and C#. • Easy appHcation deployment: Simply copy the application to the server. Configuration is achieved via XML files, and there is no need to register any components. • Dynamic update of running appHcation: Compiled DU. files can be updated without restarting the web server. • Multiple device support with one set of code: You no longer have to worry about different browser implementations. ASP. NET takes care of it all for you, no matter what browser is used. Migration Considerations ASP and ASP.NET applications can run simultaneously on the same server with out adversely affecting each other. This is primarily because the two systems have separate processing engines: the ASP. NET processes ASPX files and ASP processes ASP files. This means that the web site can be upgraded flexibly and with no downtime. The same is true for mixing components used by ASP pages. For example, you can migrate some pages to ASP.NET while they continue to work with ADO 2.5. It is important to remember that although your application can contain both ASP and ASP.NET pages, you cannot share state variables stored in the intrinsic Session or Application objects. You must either duplicate this information in both systems or come up with a custom solution until your application is fully migrated 374 Migrating ASP to ASP.NEI' All authentication and authorization is done via XML sections in your application's web. config file. This file can specify many parameters to configure each ASP.NET application running on your server. The server is configured using the machine. config file shipped with ASP.NET. Each file directory can have its own web. config file, which sets parameters for that directory and its sub directories. The web. config file takes effect when saved, so there is no need to restart the server. All named subroutines and functions must be wholly contained within <script> tags. Therefore, you must replace the<% %>tags with <script language = "vb" runat = "server"> </script>. However, any code within the<%> code delim iters is still valid and will still be executed in an ASP. NET page. For example, the following will work: <script language="vb" runat="server"> Sub RenderMe() Response.Write("<H3> This is HTML text being rendered. </H3>") End Sub </script> <% Call RenderMe() %> The Option Explicit statement is now the default, so all variables must be declared. The pages now have directives that control caching, language, and so on. Note the following attribute of the required <%@Page%> directive: <%@Page aspcompat=true Language=VB%> Using the aspcompat attribute will force your page to use components in an ASP-compatible fashion, for example, ADO rather than ADO.NET. Note also that the variant data type is gone (more specific types must be used), the Let and Set statements are no longer needed, and there are no default properties. ASP.NET uses active server objects much like ASP 3.0, but with one major update: You can now include server objects with every control of a web page. In addition, you can now easily write your own server-side controls. The Example Web Site The case study uses a modest HTML page that contains all the elements relevant to an ASP.NET migration, including JavaScript, data access code, and a calendar. You will make the changes in a text editor rather than in Dreamweaver so you can see the details up close. We will use VB .NET in the example. 375 Appendix The example web site is a language school's exam reservation service. The user selects an exam subject and a date, and the page returns the number of seats available for the exam. Business rules enforced in the Access database dic tate that a maximum of five students can take the exam on a certain date. The user's input is checked for completeness via JavaScript. A Cascading Style Sheet is also used to format the page. Figure A-1 shows the sample output. [X,um dutf.' JV.t1l·1ll1hty I I n ° II ' /1 o '• 1 "" ,.,• ,r o ,, • ..(._. 1 - I !I•'"' ' '1 o •'• Is :::1 •••B·lzooz Figure A -1. The example web site ASP to ASP.NET Web Form Controls When you migrate an ASP page to an equivalent ASP.NET page, it is a good idea to keep the page functional so you can test the page after each change. Keep a copy of the page at each stage of the migration, and give it a version ffie name and number suffix to show the progression. Table A-1 is a summary of the exam ple ffie's progression through the course of this appendix. Table A -1. Example Web Site Files File Nae Description homeasp.asp The original web page homeaspo.aspx The web page with ADO migrated to ADO.NET homeaspl.aspx The web page with ADO migrated to ADO.NET and a query result assigned to an asp: Label control homeasp2.aspx The web page with databound asp:DropDownlist control and code 376 Migrating ASP to ASP.NET Table A-1. Example Web Site Files (continued) File Nae Description homeasp3 .aspx The web page with asp: Calendar control and handling code homeasp4.aspx The web page with web server validation of selected date homeasp4upgrade.aspx The web page with improved interface using advanced ASP.NET features homeasps.aspx The completed page web server styles and links 1. Make a copy of the file homeasp. asp and name it homeaspo. aspx. Then open the file in an editor and in your browser. Ordinarily, you would start migration with simple things like images and links, but with this type of migration it is easier (and more instructive) to start with what ever error is displayed in the browser first. The .NET Framework offers help via the following directive on the top of the page: <%@ Page Language="vb" Debug="true" %> 2. This will enable you to put meaningful messages on the browser when there is an error. Set debug="false". 3. Open the ASPX page in your browser, as shown in Figure A-2. Compilation Error 0eJcrtpt5o n:Mm1»0CCU'I'«tilll.lt'911e~04•reao.rcoteel\hCII0$11t"ACettitrec,.ett,.Aenere'dew'l'lelelo¥wt'9 .,.eiljr;tiiTGfddilhendiiiOC:ffy'(fiU' ICU"OIICGr:a.IIBJI'CP'iltllly. lource frron L. in• IS: Din r-s L;ne 86: \.;1'!1! &1: liM 81' '-ine eSi t Figure A-2. An error caused by the syntax changes in ASP.NET 4. Add a reference pointing to the ADO.NET component, just below the <%@ Page %> directive. <%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.OleDb" %> 377 Appendix Next, you will change the data-accessing code. The easiest way to demon strate the changes is to show the old and new code side by side. The following tables show the original ADO code in the left column and the replacement ADO.NET code in the right column. Table A-2 shows the opening declarations. Table A-2. Comparison ofADO Code and ADO.NET Code ADO Declarations (homeasp.asp) ADO.NET Declarations (homeaspo.aspx) <% If Len(Request.QueryString("course"))>O <script runat="server"> Then Sub Page _load() Dim conn Dim mystring as String Dim strConn myString = Request.QueryString("course") Dim rs If (myString<>Nothing) Then Set conn = Dim conn As OleDbConnection Server.CreateObject("ADODB.Connection") Dim sqlCommand As Set rs = OleDbDataAdapter Server.CreateObject("ADODB.Recordset") Dim strConn As String Dim rs As Dataset Dim filepath as String Dim stillFree as String Dim rr as Datarow Dim rt as Datatable Dim rc as Datacolumn The ADO.NET code is enclosed in <script> tags, it has been assigned to run within the intrinsic Page_Load subroutine, and each variable has been explicitly declared.