Hello World: Anatomy ASP.NET Project

zhaozj2021-02-08  234

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 the same. I have learnt and programmed a variety of new languages ​​since then, and my first attempt at each new programming language has been to greet 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 be introduced to THIS LANGUAGE THAN WITH A HELLO World?

In this article, I will 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 you to support the notion of a Web Application, and we will dig INTO the DETAILS OF A ASP .NET PROJECT.

A note about the attached code is due here. Due to the multiple files that 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 you create an empty ASP .NET Project and add these files to your project. A zipped version of the source code is available for Download from Here.

How to create an asp .Net Project

Creating an ASP .NET project is fairly simple. You will need to have the Beta 1 version of Microsoft Visual Studio .NET. In reality, you do not need this tool, it is possible to open up your favorite editor and start typing, but VS .NET provides a whole lot more than your simple Notepad. I will focus on creating ASP .NET projects using Microsoft VS .NET Beta 1. In order to create an ASP .NET project, follow these 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 this case) and voila! You have just Created Your First ASP .Net Project. Rename The Webform1.aspx To A More Meaningful Name. in this sample, i Have renamed it to helloWorld.aspx and this is how I will refer to it..

FILES generated by vs .net

When you create an ASP .NET project, VS .NET by default creates the following main files for you. You should be able to view and open these files using the Solution Explorer. Let me start with a list of these files and a quick description Of this. in the next section, I will deep dress, INTO Each File and try to explain Each in More Detail.

AskHELLOWORLD.DISCO

Used for Dynamic Discovery Of Web Services

Config.Web

Contains the configuration settings for 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 Core Corresponding To The Global.asax

HelloWorld.aspx (by Default, this file is called Webform1.aspx)

Blank by Default. This is where your asp / html controls go in.

HelloWorld.vb

This Contains The Event Handling Code for the Corresponding .aspx FileStyles.css

Style Sheet That Can Be Used Across the Application

In addition to these, VS .NET also creates the following files that are used internally by VS .NET. These contain details about the project settings etc. I would recommend you to open these in notepad and view them in order to understand how VS. Net Uses these, But Be Careful Not To Modify, Modifying these Files CAUSE VS .NET To NOT Recognize Your Project and not becomle to open the solution.

AskHELLOWORLD.SLN

AskHELLOWORLD.SUO

AskHELLOWORLD.VBPROJ

AskHELLOWORLD.VBPROJ.Webinfo

Digging Into Code

Now That We Have Covered The Basics, ITS Time to Dig Into The Real Code.

Please enter your name

This is what the helloworld Webform Looks Like. It asks you for your name and then proceds to greet the world "hello world" Followed by Your Name in Parenthes.

Hello World (by kaushal)

Now That We know what the code does, lets deto 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 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 UI is as simple as dragging and dropping these controls and setting their properties in the Properties window (again a feature inspired by the VB 6.0 Properties window). I will take certain sections of the generated code and discuss this. If you want to see how these sections fit together, or would like to look at the code in its entirety, I would suggest that you open the code (from the zip file) in VS .NET and Follow Along.page Level Attributes

THE FIRST Thing You Will Notice when Open Up The HelloWorld.aspx in An Editor Is this Line:

<% @ Page language = "vb" codebehind = "helloworld.vb" inherits = "asphelloworld.helloworld"%>

This statement describes certain page level attributes for this aspx file. ASP .NET introduces a new syntactical construct <% @ ...%> which is used for specifying attributes. Every aspx file will have a statement similar to this that 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 only contains the HTML tags and server controls, while all the code needed to process the events that are fired by these controls are contained in a separate file. This allows the separation of effort between graphic designers who could be building beautiful pages and software engineers who 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 line asp file can attest to this.there Are Too Many ASP .NET Attributes That Can B E Covered Here, SO I Will Concentrate On Some of the Most Important Ones.

Language: This tells the ASP .NET compiler (! Yes ASP .NET is compiled) that the language used is VB Currently ASP .NET supports VB, JavaScript and C # CodeBehind:.. Determines the file that contains the event handling code for this aspx file Inherits:. Determines the class that will contain methods to handle events on this page AspCompat:. Determines whether this page is backward compatible with ASP Buffer:. Determines whether page level buffering is enabled EnableSessionState:.. Determines whether SessionState is enabled ErrorPage: DETERMINES The URL TO BE Redirected to in The Event of An Unhandled Exception.

ASP .NET Controls

. These controls form the heart of ASP .NET These are rich server side controls that can be used to build a powerful UI on the web similar to Windows based user interfaces . Please enter Your name

As you can see in the code above, the HelloWorld example uses four ASP .Net controls Note the "asp:". Prefix for each 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 will be used as a submit button. These server 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 be written to take action depending on what event is fired for what control This offers much more functionality and flexibility that standard HTML controls Currently ASP .NET supports the following types on server side controls..:

HelloWorld.vb Details

This file contains the code that supports the ASP .NET controls from the HelloWorld.aspx file. This code is identical to VB code, 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 since the code in its entirety.importing namespaces

Imports system

Imports system.componentmodel.design

Imports system.data

Imports system.drawing

Imports system.Web

Imports System.Web.SessionState

Imports System.Web.ui

Imports System.Web.ui.WebControls

Imports System.Web.ui.htmlControls

Imports Microsoft.visualBasic

.NET introduces the concept of namespaces. Namespaces are essentially a collection of types and functionality. VB .NET introduces the Imports statement that is used to references assemblies outside your project. Most of the namespaces that are imported here are from 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 you to access the public elements (classes, functions, methods, etc) in that namespace. In our example, VS .NET automatically imports these namespaces to add support for Web Controls. you could easily add to this default list. for example, if you wanted to add XML manipulation support to your code, you can add the "Imports System.xml "Line to your code.

HelloWorld Class

Public class helloworld

Inherits System.Web.ui.page

Protected withevents lblname as system.Web.ui.WebControls.label

Protected withevents btnsubmit as system.Web.ui.WebControls.button

Protected withevents txtname as system.Web.ui.WebControls.textbox

Protected Withevents Lblhello As System.Web.ui.WebControls.Label

Public Sub btnsubmit_click (Byval e AssyiStem.EventArgs)

TXTNAME.Visible = FALSE

LBLNAME.Visible = FALSE

btnsubmit.visible = false

lblhello.text = "Hello World (by" txtname.text ")"

lblhello.visible = true

End Sub

Protected Sub Webform1_Load (Byval Sender As System.Object, ByVal E

As system.eventargs)

'Evals True First Time Browser Hits The Page

IF not ispostback.

lblhello.visible = false

END IF

End Sub

END CLASS

VB .NET is a purely object oriented language. (Of course, you could still write non-OO code and not use the OO features that VB .NET provides, but that defeats the purpose). Thus, all the code in the above figure resides in a class definition. We declare a public class named HelloWorld that forms the backbone of our HelloWorld.aspx page. All the code behind an ASP .NET page is derived from the System.Web.UI.Page class, which contains the framework Code for ASP .NET PAGES.

The four controls that are used on this page as defined as protected members of this class and the type of each controls is referenced from the System.Web.UI.WebControls namespace. The WithEvents keyword is used to specify that the object variables will respond to .

The crux of the logic in an ASP .NET page will live in the event handling code for one or more server side controls. In our trivial example, we handle two events. The first event is for the page load in which we simply hide the label that will be used to display the Hello World message. When the user clicks on the Submit button, this will 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 is all the code That Necessary to supportur 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 you with the IIS Metabase know that maintaining and modifying configuration was not who have dealt exactly a breeze. 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 will focus only on some of the settings that are generated by VS .NET by default.

enabled = "false"

RequestLimit = "0"

Pageoutput = "false"

Tracemode = "sortbytime"

/>

Inproc = "True"

Usesqlserver = "false"

Cookieles = "false"

TIMEOUT = "20"

Server = "Localhost"

Port = "42424"

/>

The release 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 is 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 is possible for ASP .NET to redirect the page to an error page whenever an unhandled exception occurs. Further, it is possible to define separate custom error pages for each .

Trace

Those of you who have tried debugging traditional ASP programs probably used more Response.Write statements than you cared to. ASP .NET introduces an elegant way of adding trace / debug information that is consistent and configurable. By turning on Trace in the Config.web file, it is possible to have Trace statements throughout the code that 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 use the ASP Session variables, only to realize that the application will be running on a WebFarm without server affinity. In these cases, you end up not using the ASP Session, or you come up with your own implementation of session state. ASP .NET introduces a new way of handling Session data. you could use session the way it is used currently, or you could use a separate process (on the same machine or a separate machine) to store session data. you could even persist session data in a database. Again, this is entirely declarative and you do not have to change a single line of code to support this. This is a very powerful way to handle session data without having to implement a Custom Implementation.ConClusion

When i wrote my first hello world in c, I buy the following code

#include

int main ()

{

Printf ("Hello World");

Return 1;

}

My next Hello World was 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 increasing. The same holds true for the Hello World that I wrote for ASP .NET. As you can see from the above code, the code needed to write a Hello World example takes far more than 7 lines. However, most of the code that is present is to support the framework and the plumbing needed to build complex real world applications. This is certainly true for ASP .NET, which is a huge huge improvement over traditional ASP, and Microsoft has brought VB-like development to the web. In future articles I will Dig Deeper INTO The GUTS OF ASP .NET.

Happy .netting!

Additional information: an overview of asp.net transition to asp.net - Part 1 Server Controls

转载请注明原文地址:https://www.9cbs.com/read-1646.html

New Post(0)