ASP.NET


A lot of the .NET Framework has to do with Web programming. In the past, developers working with Microsoft's Web Server (IIS) had a choice of writing CGI programs or ISAPI extensions. ISAPI extensions were DLLs, as opposed to CGI programs that were done as EXEs.

DLLs perform better than EXEs in IIS. By writing an ISAPI extension, a programmer could intercept requests made to the Web server, analyze the request, and then tell the server how to respond to the client. So rather than just returning static content to a client, a programmer could return dynamic content with ISAPI extensions. For example, a developer could read a database and use the database information to build the response. However, it was nearly impossible to write a good ISAPI extension. It required knowledge of C++ and writing code that could handle many simultaneous requests.

For that reason, Microsoft wrote one general-purpose ISAPI extension called Active Server Pages (ASP). ASP.DLL executes when clients request documents that end with the .asp extension (other extensions also come into play, but .asp is the main one). A developer would interact with ASP.DLL by writing a script in VBScript or JavaScript. A client would make a request from the server in the form of www.server.com/hello.asp.

IIS would get the request and ask ASP.DLL to handle it. ASP.DLL would then process the script in the page requested (hello.asp). This script would then use ASP-provided objects such as the Request object and the Response object to interact with the Web server. A developer might write a script such as the following:

 Welcome to Widget's USA Bank: <% 'code here to open a database table and 'read client's balance 'then store balance in a variable like '"bal" %> Your current balance is <%=bal%> 

The code would have portions in HTML and then placeholders for code to be processed by ASP.DLL. Code segments would be enclosed between <% %> tags. The client would never see the code for the page, only the HTML generated from the code in his or her browser.

ASP had three main limitations, however. The first was speed. Every time a page was requested, the extension would have to process the script. The second was that ASP had no knowledge of HTML. It enabled the developer to read a request and build a response, but if you wanted to include a table to display figures as part of your response you needed to know how to write the HTML for a table. For example you would need to write a script like the following for a table, and as you'll see, it's a lot of work:

 <% 'first open database and create a recordset 'read all records from the authors table in 'the pubs database Dim rs Set rs = CreateObject("ADODB.RecordSet") rs.Open "select au_id,au_lname from authors", "provider=sqloledb;server=.;initial catalog=pubs;user id=sa;password=;",0,1  Response.Write("<table   cellspacing=""0"" rules=""all""   border=""1"" id=""DataGrid1"">")   'print headings   Response.Write("<tr>")   Response.Write("<td>ID</td><td>Last   Name</td>")   Response.Write("</tr>")   'loop through all the records. Display   'records in an HTML table.   While rs.EOF <> True   Response.Write("<tr>")   Response.Write("<td>" & rs("au_id")   &   "</td>")   Response.Write("<td>" &   rs("au_lname") &   "</td>")   Response.Write("</tr>")   rs.MoveNext   Wend   'finish the table   Response.Write("</table>")  %> 

I've highlighted the lines that build the table. The rest of the script makes a connection to a SQL Server database and reads all the records from the authors table in the pubs database (one of the sample databases installed with SQL Server).

The third limitation with ASP was that the languages such as VBScript and JavaScript weren't full-featured languages; they lacked some of the essential features of other object-oriented languages.

To overcome the problems associated with ASP, Microsoft created ASP.NET, a brandnew ISAPI extension that works with the .NET Framework. It's mostly contained in aspnet_isapi.dll, one of the DLLs installed when you install the .NET Framework. The function of this DLL is to handle requests for documents that end with the .aspx extension (among others). It can be installed alongside ASP. ASP.NET pages are written in one of the languages for the .NET Framework. They can be written in C#, VB.NET, or even a new version of JavaScript.

The advantage of ASP.NET is that you can use all the features of the language; C# for ASP.NET is no different than C# for desktop applications. Another advantage is that the ASP.NET pages are compiled the very first time they are requested. From then on, any other requests are handled by the compiled version of the page. This makes them as much as three or four times faster than traditional ASP pages.

Finally, ASP.NET lets you use two styles of programming. You can rely on knowing HTML and build things like tables from scratch, or you can develop using Web Forms. Web Forms are pages in which you specify the location of full controls. Say "place table here," and the architecture then generates the HTML necessary to produce the element. Here is an example of a Web Form:

 <%@Page Language="C#" %> <%@Import Namespace="System.Data.SqlClient"%> <form id="Form1" method="post" runat="server">  <asp:DataGrid id="DataGrid1"   runat="server"/>  </form> <script runat="server"> private void Page_Load(object sender,                       System.EventArgs e) {   //first open database and create a recordset   //read all records from the authors table in   //the pubs database   SqlConnection conn = new SqlConnection( "server=.;database=pubs;uid=sa;pwd=;");   SqlCommand cmd = new SqlCommand(   "select au_id,au_lname from authors",conn);   conn.Open();   SqlDataReader rs = cmd.ExecuteReader();  DataGrid1.DataSource=rs;   DataGrid1.DataBind();  } </script> 

Look at the highlighted code lines in the preceding example. Notice first the line that starts with the tag "asp:" This tag tells the ASP.NET code generator to declare a WebControl field called DataGrid1 of type System.Web.UI.WebControls.DataGrid1 . The DataGrid control knows how to draw its contents as an HTML table. To set the contents of the grid we use another feature of the DataGrid control called databinding. (The lines that use the databinding feature are also highlighted.) With databinding, you set the control's datasource property to a collection of records like a datareader (the equivalent of a read-only forward-only recordset in old ADO) and then call the DataBind function. The grid takes care of setting its contents to the records in the datareader. (A complete discussion of databinding is beyond the scope of this book.)

In addition to Web Forms knowing HTML, they simulate classical desktop development. ASP development used to require that you learn one way of programming for desktop applications and a different one for Web applications. With ASP.NET, Web programming uses the same event-driven style as desktop application programming. In the example above, when the page loads, the Page_Load event fires, similar to the Load event in typical Visual Basic forms and even the new .NET Windows Forms. All these new features make ASP.NET a much better choice for Web development than its predecessor.



C#
C# & VB.NET Conversion Pocket Reference
ISBN: 0596003196
EAN: 2147483647
Year: 2003
Pages: 198
Authors: Jose Mojica

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