Hello World:an Anatomy of an ASP. NET Projectby Kaushal Sanghavi
Introduction
Ever since Kernighan and Ritchie wrote a program to display ' Hello world ' in the ' C language, programming has never-been th E Same. I have learnt and programmed a variety of new languages since then, and my I-attempt at each new programming has been to greet of the world with a "Hello world". ASP. NET is not just a new revision of ASP. It is not ASP 4.0. It is a completely new paradigm and a new programming model and language. What better way to is introduced to this language than with a Hello world?
In this article, I'll attempt to write a simple ' Hello world ' and explain all the ' code that ' goes behind this. ASP. NET generates a lot of code and files for your to support the notion of a Web application, and we'll dig into the de Tails of an ASP. NET project.
A Note about the attached code are due here. Due to the multiple files this VS. NET creates, the references to drives, and folders, it turned out to be very difficult For me to zip up all the code and link it to this document. It would have been next to impossible for someone to download this code and get it working ' out of the box '. As a result, I am only attaching the helloworld.aspx and Helloworld.vb files and I would recommend your create an empty ASP . NET project and add this files to your project. A zipped version of the source code is available to download from. How to Create an ASP. NET Project
The
Creating an ASP. NET project are fairly simple. You'll need to have the Beta 1 version of Microsoft Visual Studio. NET. In reality, your don ' t need this tool, it's possible to open up your favorite editor and start typing, but VS. NET provide s a whole lot more than your simple Notepad. I'll focus on creating ASP. NET projects using Microsoft VS. NET Beta 1. In order to create an ASP. NET project, follow this simple steps. Chose "New Project" from VS. NET IDE. Under the "Project Type", select "Visual Basic Projects" and chose the "Web application Template". Enter the name of your ASP. NET project (Asphelloworld in-case) and voila! You have just created your. Rename the WebForm1.aspx to a more meaningful name. In this sample, I have renamed it to helloworld.aspx and this are how I'll refer to it in this article. Files generated by VS. NET
When you create an ASP. Project, VS. NET By default creates the following main files to you. You should are able to view and open these files using the Solution Explorer. Let me-start with a list of the files and a quick description of this. In the next section, I'll deep deeper into each of the file and try to explain all in more detail. Asphelloworld.disco Used for dynamic discovery of Web Services config.web Contains The configuration settings to this web Application Global.asax Similar to the Global.asa file. Contains Global events for this WEB application Global.vb this Contains the event handling code corresponding to the Globa L.asax helloworld.aspx (by default, this file was called WebForm1.aspx) Blank by default. This is where your asp/html controls. Helloworld.vb This contains the event handling code for the corresponding. aspx file Styles.css Style sheet which can be us Ed across the application
In addition to this, VS. NET also creates the following files, are used internally by vs. net. These contain details about the project settings etc. I would recommend you to open this in Notepad and view them the to understand how VS. NET uses this, but be careful Not to modify. Modifying these files are could cause VS. NET to not recognize your project and is able to open the solution. ASPHelloWorld.sln Asphelloworld.suo asphelloworld.vbproj ASPHelloWorld.vbproj.webinfo digging into Code
Now so we have covered the basics, its time to dig into the real code.
This is what the HelloWorld WebForm looks. It asks for your name and then proceeds to greet the world ' Hello world ' followed by your name in parenthesis.
Now so we know what the code does, lets dive into the code. helloworld.aspx Details
Lets start with digging into the details of the "ASPX" file that has been generated. Keep in mind, I have not written a single line of the HTML for this. This is a very vb-sque experience. VS. NET provides a standard Toolbox that contains WebForm controls, HTML controls, Server controls etc, and building the U I is as simple as dragging and dropping these controls and setting their properties in the Properties window (again a feat Ure inspired by the VB 6.0 Properties window). I'll take certain sections of the generated code and discuss this. If you are want to "how" to "sections fit together," or "would like", "at the" code in its entirety, I would suggest tha T you open the code (from the zip file) in VS. NET and follow along. Page level Attributes
The the ' the ' thing you'll notice when your open up the helloworld.aspx in a editor is this line:
<%@ Page language= "vb" codebehind= "Helloworld.vb" inherits= "Asphelloworld.hellowor LD '%> |
This statement describes the certain page level attributes to this ASPX file. ASP. NET introduces a new syntactical construct <%@ ...%> which is used for specifying. Every aspx file would have a statement similar to this so describes attributes such as the class that contains the event Handler code, the name of the file containing the code, the language etc. This is necessary because ASP. NET cleanly distinguishes the presentation from the code. The ASPX file is contains the HTML tags and server controls, while the "code needed to process" the events that are fi Red by this controls are contained in a separate file. This is allows the separation of effort between graphic designers who could is building beautiful pages and software S. Could be writing beautiful code. The current ASP model mixes the HTML tags and controls with code, and anyone trying to read, maintain, or debug a 5000 Lin E ASP file can attest to this.
There are too many ASP. NET attributes that can is covered here, so I'll concentrate on some of the most important. Language:this tells the ASP. NET Compiler (yes! ASP. NET is compiled-language used is VB. Currently ASP. NET supports VB, JavaScript and C #. Codebehind:determines the "file" contains the event handling code for this ASPX file. Inherits:determines the class that would contain methods to handle events on this page. Aspcompat:determines whether this page was backward compatible with ASP. Buffer:determines Whether page level buffering is enabled. Enablesessionstate:determines whether sessionstate is enabled. Errorpage:determines the URL to being redirected to the event of a unhandled exception. ASP. NET Controls
These controls form the heart of ASP. NET. These are rich server side controls the can is used to builds a powerful UI on the web similar to Windows based user Inter Faces.
As you can the code above, the HelloWorld example uses four ASP. Net controls. The "asp:" prefix for all control. This is what distinguishes ASP. NET controls from standard HTML controls. We use two labels to display static messages, a TextBox to allow the user to input the name, and a button that is use D as a submit button. These servers side controls fire events very similar to the way Windows controls fire events in a VB 6.0 program. These events can be trapped and code can are written to take action depending on what the event is fired for what. This offers the functionality and flexibility that standard HTML controls. Currently ASP. NET supports the following types on server side controls: <asp:button> <asp:imagebutton> <as p:linkbutton> <asp:hyperlink> <asp:textbox> <asp:checkbox> <asp:radiobutton> <asp: image> <asp:label> <asp:panel> <asp:table>Helloworld.vb Details
This file contains the "code" supports the ASP. NET controls from the Helloworld.aspx file. This code is identical to VB code and in fact it is VB code. Lets start with some code excerpts, again I would recommend you to have VS. NET Open and the project loaded and have the C Ode in its entirety. Importing Namespaces
System
System.ComponentModel.Design
system.data
system.drawing
system.web
System.Web.SessionState
System.Web.UI
System.Web.UI.WebControls
System.Web.UI.HtmlControls
Microsoft.VisualBasic
|
. NET introduces the concept of namespaces. Namespaces are essentially a collection of types and functionality. VB. NET introduces the Imports statement it used to references assemblies outside your project. Most of the namespaces that are imported here are to the System namespace, but you could import any namespace as long as It lives in a public assembly. Importing a namespace is similar to adding a reference to a DLL in VB 6.0. It allows access the public elements (classes, functions, methods, etc.) in that namespace. In our example, the VS. NET automatically imports these namespaces to the add support for Web Controls. You are could easily add to this default list. For example, if your wanted to add XML manipulation support to your code, you can add the ' Imports System.Xml ' line to your Code. HelloWorld Class
HelloWorld System.Web.UI.Page lblname System.Web.UI.WebControls.Label Btnsubmit System.Web.UI.WebControls.Button txtname System.Web.UI.WebControls.TextBox Lblhello system.web. Ui. Webcontrols.label btnSubmit_Click (sender, E System.eventa RGS) txtname.visible = lblname.visible = btnsubmit.visible = Lblhello.text = "Hello Wor
LD (by "+ txtName.Text +") "lblhello.visible = Webform1_load (Sender System.Object, E
System.EventArgs) IsPostback lblhello.visible = |
VB. NET is a purely object oriented language. (Of course, you could still write Non-oo code and don't use the OO features this VB. NET provides, but that defeats the Purp OSE). Thus, all of the code in the above figure resides in a class definition. We declare a public class named HelloWorld this forms the backbone of our helloworld.aspx page. All of the code behind an ASP. NET page are derived from the System.Web.UI.Page class, which contains the framework code for A SP. NET pages.
The four controls that are used on this page as defined as protected members of this class and the type of all controls I s referenced from the System.Web.UI.WebControls namespace. The WithEvents keyword is used to specify the object variables would respond to triggered events. In addition to this data members, your could add any data members, and the want to and specify the access modifiers (p Ublic, private, protected) for the data members.
The crux of the logic in an ASP. NET page would live in the event handling code for one or more server side controls. In our trivial example, we handle two events. The ' the ' The ' for ' page load in which we simply hide the ' would be used to display the ' Hello World Messag E. When the user clicks on the Submit button and this is fire the event handled by the btnSubmit_Click method. In this method, we simply read the value entered in the TextBox and display the "Hello World" message. We also hide some of the controls that are no longer needed. This are all the code so necessary to support we Hello world sample. config.web Details
With ASP. NET, Microsoft introduces the notion of a human readable and modifiable configuration file for Web applications. Those of who have dealt with the IIS Metabase know that maintaining and modifying configuration wasn ' t exactly a Bree Ze. ASP. NET introduces an XML file based configuration scheme, which is extremely powerful. The amount of details in the file warrants a whole new article, so I'll focus is on some of the "settings" that are gene Rated by VS. NET by default.
These are some of the default settings generated by VS. NET.
Compilation Mode This settings tells The.NET compilers to generate debug information for the. aspx files. This is usually set to True in the development cycle, but once the application are moved over to Production, this should be Set to False. Turning off this setting greatly improves the runtime performance of the system. Custom Errors ASP. NET introduces a declarative way of exception handling. By turning on CustomErrors, it's possible for ASP. NET to redirect the page to a error page whenever an unhandled except Ion occurs. Further, it is possible to define separate custom error pages for each HTTP error code that could occur. This is a very powerful feature which can avoid ugly errors being to the user. Trace those of your who have tried debugging traditional ASP programs probably used more Response.Write statements than Cared to. ASP. NET introduces an elegant way of adding trace/debug information-is consistent and configurable. By turning on Trace in the Config.webFile, it is possible to have Trace statements throughout the code so can be turned on or off by this switch. Further, you can determine where the trace output is displayed and have levels of tracing such as Fatal or informational. Session state There have been so many occasions where I wished I could with the ASP session variables The application is running on a webfarm without server affinity. In these cases, your end is not using the ASP session, or your come up and your own implementation of session state. ASP. NET introduces a new way of handling session data. You are could use sessions the way it is used currently, or your could use a separate process (on the same machine or a separate Machine) to the store session data. You are could even persist session data in a database. Again, this are entirely declarative and you don ' t have to the change a single line of code to support this. This is a very powerful way to handle session data without has to implement a custom ImplEmentation. Conclusion
When I wrote the following code of my I-Hello world in C, I used
#include <stdio.h>
int main ()
{
printf ("Hello World");
return 1;
}
|
My next Hello world is in C + +, which took a few more lines, and then I moved over to Windows programming in SDK, Windows Programming in MFC, COM programming, COM programming in ATL and slowly the complexity of the Hello World Programs kept Inc Reasing. The same holds true for the "Hello World" I wrote for ASP. As you can the above code, the code needed to write a Hello World example takes far more than 7 lines. However, most of the code this is present are to support the framework and the plumbing needed Applications. This is certainly true for ASP., which are a huge huge improvement over traditional ASP, and Microsoft has brought vb-l IKE development to the Web. In future articles, I'll dig deeper into the guts of ASP.
Happy. Netting! Additional Information: An Overview of asp.net transitioning to Asp.net-part 1 Server Controls