Sie sind auf Seite 1von 31

ASP.NET 3.

Design & Presented By:


SHOAIB M.G.

1.Introduction To ASP.NET

Understanding Microsofts Web Technologies


In the beginning, the World Wide Web (WWW) was flat. It was an electronic

library where academics and scientists posted dissertations and dusty


data for reading with clunky, text-only browsers. With the advent of graphical browsers, the consumer-oriented Web took off. Content became vastly more colorful. Anyway, the Web has evolved as a rich, interactive, and personalized medium. In the new version of Web (Web 2.0), functional pages arent enough. User experience is hot, and sites are cool. Looks at Microsofts tools and technologies for creating and delivering engaging Web content.

Introducing the Content-Creation Tools


Microsoft has a range of tools for authoring Web pages that appeal to several
skill levels. Some tools are more suited to Web page design, while others are more appropriate to programming.

Microsoft Office (Including Word 2007):


When Bill Gates realized that Microsoft was lagging on the Internet front, the word went out to integrate Web support into every product. As a result, you can save Excel spreadsheets, Word documents, and PowerPoint slides as Web pages.Many companies use the Office suite to place information on their intranet because most employees are comfortable in Word and Excel. These tools are

quite adequate for creating static Web content that some call brochure ware.
Although somewhat bloated, the pages are faithful reproductions of the original document especially when viewed in Microsofts latest Internet Explorer browser.

Expression Web:
Expression Web took over from Microsoft FrontPage as the content editor for professional designers. Although some see Expression as an advanced word processor for HTML pages, its actually much more, thanks to many important

tools for Web designers. These tools include file management, link checking,
style editing, and drag-and-drop support for HTML and ASP.NET controls. Expression Web inherited the excellent split-view editor from FrontPage that lets you work in graphical and source code modes at the same time. The feature is so well done that Microsoft yanked the HTML editor from Visual Web Developer and substituted the superior Expression/FrontPage version.

Expression Blend:
Expression Blend is mainly for the ponytail set (artistic types who prefer Macs) to create vector-based, animated, and three-dimensional graphics much the way they do in Photoshop. Blend has a rich set of brushes, palettes, paint buckets, text, gradients, timelines, and event triggers for those with the skill to take advantage of them. The XML-based files that Blend generates work in Windows Presentation

Foundation (WPF) applications that run on Windows and in cross-platform


Silverlight apps for the Web. (For more on Silverlight, see the section later in this chapter. Blends user interface (UI) is dim and funereal a far cry from the cheerful Windows XP or glitzy Windows Vista UI. The theory is that a drab, flat design environment doesnt distract an artiste from his or her canvas.

Visual Web Developer (Visual Studio.Net):


Visual Web Developer (VWD) is the premier tool for programming Web sites

on the Microsoft platform. Just as Word is part of the Office suite, VWD is
part of the bigger Visual Studio 2008 suite. Visual Studio includes Visual Basic .NET, Visual C#, and many other tools. Visual Studio comes in several versions to target teams of developers, database designers, testers, and system architects. As an integrated development environment (IDE), Visual Web Developer helps you assemble and build the key elements of a Web application, including Web pages, images, controls, databases, style sheets, and, of course, the

programming logic.

ASP V/S ASP.NET


ASP (Active Server Pages):
A classic ASP page is a hodgepodge of HTML and server-side script code. If you have never worked with classic ASP, understand that the goal of ASP is to dynamically build HTML on

the fly using a server-side script using a small set of COM objects and a bit of elbow grease.
For example, you may have a server-side VBScript (or JavaScript) block that reads a table from a data source using classic ADO and returns the rows as a generic HTML table.

Problems with Classic ASP:


While many successful websites have been created using classic ASP, this architecture is not without its downsides. Perhaps the biggest downside of classic ASP is the same thing that makes it a powerful platform: server-side scripting languages. Scripting

languages such as VBScript and JavaScript are interpreted, type less entities that do not
lend themselves to robust OO programming techniques.

Another problem with classic ASP is the fact that an *.asp page does not yield very

modularized code. Given that ASP is a blend of HTML and script in a single page, most
ASP web applications are a confused mix of two different programming techniques. While it is true that classic ASP allows you to partition reusable code into distinct include files, the underlying object model does not support true separation of concerns. In an ideal world, a web framework would allow the presentation logic (i.e., HTML tags) to exist independently from the business logic (i.e., functional code).

A final issue to consider here is the fact that classic ASP demands a good deal of
boilerplate, redundant script code that tends to repeat between projects. Almost all web applications need to validate user input, repopulate the state of HTML widgets before emitting the HTTP response, generate an HTML table of data, and so forth.

Major Benefits of ASP.NET 1.0


The first release of ASP.NET (version 1.x) did a fantastic job of addressing each of the limitations found with classic ASP. In a nutshell, the .NET platform brought about the following techniques to the Microsoft web development paradigm: ASP.NET provides a model termed code-behind, which allows you to separate

presentation logic from business logic.


ASP.NET pages are coded using .NET programming languages, rather than interpreted scripting languages. The code files are compiled into valid .NET assemblies (which translates into much faster execution). Web controls allow programmers to build the GUI of a web application in a manner similar to building a Windows Forms/WPF application. ASP.NET web controls automatically maintain their state during postbacks using a

hidden form field named __VIEWSTATE.


ASP.NET web applications are completely object-oriented and make use of the Common Type System (CTS). ASP.NET web applications can be easily configured using standard IIS settings or using a web application configuration file (Web.config).

Major Enhancements of ASP.NET 2.0


While ASP.NET 1.x was a major step in the right direction, ASP.NET 2.0 provided additional bells and whistles. Consider this partial list: Introduction of the WebDev.WebServer.exe testing web server

A large number of additional web controls (navigation controls, security controls,


new data controls, new UI controls, etc.) The introduction of master pages, which allow you to attach a common UI frame to a set of related pages Support for themes, which offer a declarative manner to change the look and feel of the entire web application Support for Web Parts, which can be used to allow end users to customize the look

and feel of a web page


Introduction of a web-based configuration and management utility that maintains your Web.config files

Major .NET 3.5 Web Enhancements:


As you would expect, .NET 3.5 further increases the scope of the ASP.NET programming model.Perhaps most important, we now have New controls to support Silverlight development (recall that this is a WPF-based API for designing rich media content for a website)

Integrated support for Ajax-style development, which essentially allows for


micropostbacks to refresh part of a web page as quickly as possible

The ASP.NET Namespaces


Core functionality (e.g., types that allow you to interact with the HTTP request and response,Web Form infrastructure, theme and profiling support, Web Parts, security, etc.) Web Form and HTML controls Mobile web development Silverlight development Ajax development XML web services

The Core ASP.NET Web-Centric Namespaces

The ASP.NET Web Page Code Model


ASP.NET web pages can be constructed using one of two approaches. You are free to
create a single *.aspx file that contains a blend of server-side code and HTML (much like classic ASP). Using the single-file page model, server-side code is placed within a <script> scope, but the code itself is not script code proper (e.g., VBScript/JScript). Rather, the code statements within a <script> block are written in your .NET language of choice (C#, Visual Basic, etc.). If you are building a page that contains very little code (but a good deal of HTML), a single-file page model may be easier to work with, as you can see the code and the markup in one unified *.aspx file. In addition, placing your procedural code and HTML markup into a single *.aspx file provides a few other advantages: Pages written using the single-file model are slightly easier to deploy or to send to

another developer.
Because there is no dependency between files, a single-file page is easier to rename. Managing files in a source code control system is slightly easier, as all the action is taking place in a single file.

The default approach taken by Visual Studio 2008 (when creating a new website solution)

is to make use of a technique known as code-behind, which allows you to separate your
programming code from your HTML presentation logic using two distinct files. This model works quite well when your pages contain a significant amount of code or when multiple developers are working on the same website. The code-behind model offers several benefits as well: Because code-behind pages offer a clean separation of HTML markup and code, it is possible

to have designers working on the markup while programmers author the C# code.
Code is not exposed to page designers or others who are working only with the page markup (as you might guess, HTML folks are not always interested in viewing reams of C# code). Code files can be used across multiple *.aspx files.

Understanding the Role of ASP.NET Directives:


The first thing to be aware of is that a given *.aspx file will typically open with a set of directives.ASP.NET directives are always denoted with <%@ ... %> markers and may be qualified with various attributes to inform the ASP.NET runtime how to process the

attribute in question.
Every *.aspx file must have at minimum a <%@Page%> directive that is used to define the managed language used within the page (via the language attribute). Also, the <%@Page%> directive may define the name of the related code-behind file (if any), enable tracing support, and so forth. documents some of the more interesting <%@Page%>-centric attributes.

In addition to the <%@Page%> directive, a given *.aspx file may specify various <%@Import%> directives to explicitly state the namespaces required by the current page and <%@Assembly%> directives to specify the external code libraries used by the site

(typically placed under the \bin folder of the website).

Debugging and Tracing ASP.NET Pages By and large, when you are building ASP.NET web projects, you can use the same debugging techniques as you would with any other sort of Visual Studio 2008 project type. Thus, you can set breakpoints in your code-behind file (as well as embedded script blocks in an *.aspx file), start a debug session (via the F5 key, by default), and step through your code. On a related note, you are also able to enable tracing support for an *.aspx file by setting the Trace attribute to true within the <%@Page%> directive (it is also possible to enable tracing for your entire site by modifying the Web.config file): insert your own trace messages into the mix, you can use the Trace property of the System.Web. UI.Page type. Anytime you wish to log a custom message (from a script

block or C# source code file), simply call the Write() method:


protected void btnFillData_Click(object sender, EventArgs e) { Trace.Write("My Category", "Filling the grid!"); }

The Role of HTTP:


Web applications are very different animals from traditional desktop applications (to say the
least).The first obvious difference is that a production-level web application will always involve at least two networked machines (of course, during development it is entirely possible to have a single machine play the role of both the browser-based client and the web server itself ). Given the nature of web applications, the networked machines in question must agree upon a particular wire protocol to determine how to send and receive data. The wire protocol that connects the computers in question is the Hypertext Transfer Protocol (HTTP).

The HTTP Request/Response Cycle


When a client machine launches a web browser (such as Opera, Mozilla Firefox, or Microsoft Internet Explorer), an HTTP request is made to access a particular resource (typically a web page) on the remote server machine. HTTP is a text-based protocol that is built upon a standard request/ response paradigm. For example, if you navigate to http://www.intertech.com, the browser software leverages a web technology termed Domain Name Service (DNS) that converts the registered URL into a four-part, 32-bit

numerical value, termed an IP address.

At this point, the browser opens a socket connection (typically via port 80 for a nonsecure

connection) and sends the HTTP request for processing to the target site. The web server receives the incoming HTTP request and may choose to process out any clientsupplied input values (such as values within a text box, check box selections, etc.) in order to format a proper HTTP response. Web programmers may leverage any number of technologies (CGI, ASP, ASP.NET, JSP, etc.) to dynamically generate the content to be emitted into the HTTP response. At this point, the client-side browser renders the HTML sent from the web server.

HTTP Is a Stateless Protocol

Another aspect of web development that is markedly different from traditional desktop
programming is the fact that HTTP is essentially a stateless wire protocol. As soon as the web server emits a response to the client, everything about the previous interaction is forgotten. This is certainly not the case for a traditional desktop application, where the state of the executable is most often alive and kicking until the user shuts down the application in question.Given this point, as a web developer, it is up to you take specific steps to remember information (such as items in a shopping cart, credit card numbers,

home and work addresses, etc.) about the users who are currently logged on to your site.
ASP.NET provides numerous ways to handle state, many of which are commonplace to any web platform (session variables, cookies, and application variables) as well as some .NETparticular techniques such as the ASP.NET profile management API

Understanding Web Applications and Web Servers:


A web application can be understood as a collection of files (*.htm, *.asp, *.aspx, image files, XMLbased file data, etc.) and related components (such as a .NET code library or legacy COM server) stored within a particular set of directories on a given web server. ASP.NET web applications have a specific life cycle and provide numerous events (such as initial startup or final shutdown) that you can hook into to perform specialized processing during your websites

operation.
A web server is a software product in charge of hosting your web applications, and it typically provides a number of related services such as integrated security, File Transfer Protocol (FTP) support, mail exchange services, and so forth. Internet Information Services

(IIS) is Microsofts enterprise-level web server product, and as you would guess, it has
intrinsic support for classic ASP as well as ASP.NET web applications.

When you build production-ready ASP.NET web applications, you will often need to interact with IIS. Be aware, however, that IIS is not automatically selected as an installation option when you install the Windows operating system (also be aware that not all versions of Windows can support IIS, such as Windows XP Home). Thus, depending on the configuration of your development machine, you may wish to install IIS before proceeding through this chapter. To do so, simply access the Add/Remove Program applet from the Control Panel folder and select Add/Remove Windows Components. Consult the Windows help system if you require further details. Assuming you have IIS properly installed on your workstation, you can interact with IIS from the Administrative Tools folder (located in the Control Panel folder) by doubleclicking the Internet Information Services applet. For the purposes of this chapter, you

are concerned only with the Default Web Site node

The Role of IIS Virtual Directories

A single IIS installation is able to host numerous web applications, each of which resides in
a virtual directory. Each virtual directory is mapped to a physical directory on the hard drive. Therefore, if you create a new virtual directory named CarsRUs, the outside world can navigate to this site using a URL such as http://www.CarsRUs.com (assuming your sites IP address has been registered with the world at large). Under the hood, this virtual directory maps to a physical root directory on the web server, such as C:\inetpub\wwwroot\AspNetCarsSite, which contains the content of the CarsRUs

web application. As you will see later in this chapter, when you create ASP.NET web
applications using Visual Studio 2008, you have the option of having the IDE generate a new virtual directory for the current website automatically. If required, you are certainly able to manually create a virtual directory by hand by right-clicking the Default Web Site node of IIS and selecting New Virtual Directory (or on Vista, simply Add Virtual Directory) from the context menu.

The ASP.NET Development Server Prior to .NET 2.0, ASP.NET developers were required to make use of IIS virtual directories during the development and testing of their web content. In many cases, this tight dependency on IIS made team development more complex than necessary (not to mention that many network administrators frowned upon installing IIS on every developers machine). Thankfully, we now have the option to use a lightweight web server

named WebDev.WebServer.exe. This utility allows developers to host an ASP.NET web


application outside the bounds of IIS. Using this tool, you can build and test your web pages from any directory on your machine. This is quite helpful for team development scenarios and for building ASP.NET web programs on versions of Windows that do not support IIS installations (such as Windows XP Home). When building a website with Visual Studio 2008, you have the option of using WebDev.WebServer.exe to host your pages (as you will see a bit later in this chapter). However, you are also able to manually interact with this tool from a Visual Studio 2008 command prompt. WebDev.WebServer.exe -?

you will be presented with a message box that describes the valid command-line options. In a nutshell, you will need to specify an unused port (via the /port: option), the root directory of the web application (via the /path: option), and an optional virtual path using the /vpath: option (if you do not supply a /vpath: option, the default is simply /). Consider the following usage, which opens an arbitrary port to view content in the

C:\CodeTests\CarsWebSite directory created previously:


WebDev.WebServer.exe /port:12345 /path:"C:\CodeTests\CarsWebSite" Once you have entered this command, you can launch your web browser of choice to request pages. Thus, if the CarsWebSite folder had a file named Default.aspx, you could enter the following URL: http://localhost:12345/CarsWebSite/Default.aspx

The Role of HTML


Now that you have configured a directory to host your web application, and you have
chosen a web server to serve as the host, you need to create the content itself. Recall that web application is simply the term given to the set of files that constitute the functionality of the site. To be sure, a vast number of these files will contain tokens defined by Hypertext Markup Language (HTML). HTML is a standard markup language used to describe how literal text, images, external links, and various HTML-based UI widgets are to be rendered within the client-side browser. This particular aspect of web

development is one of the major reasons why many programmers dislike building webbased programs. While it is true that modern IDEs (including Visual Studio 2008) and web development platforms (such as ASP.NET) generate much of the HTML automatically, you will do well to have a working knowledge of HTML as you work with ASP.NET. While this section will most certainly not cover all aspects of HTML (by any means), lets check out some basics and build a simple web application using HTML, classic (COMbased) ASP, and IIS. This will serve as a road map for those of you coming to ASP.NET

from a traditional desktop application development background.

HTML Document Structure <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head> <title>Untitled Page</title> </head> <body> </body> </html>

First of all, notice that this HTML file opens with a DOCTYPE processing instruction. This
informs the IDE that the contained HTML tags should be validated against the XHTML standard. As suggested, traditional HTML was very loose in its syntax. For example, it was permissible to define an opening element (such as <br>, for a line break) that did not have a corresponding closing break (</br> in this case), was not case sensitive, and so forth. The XHTML standard is a W3C specification that adds some much-needed rigor to the HTML markup language.

ASP.NET Page Life Cycle:

PreInit: Inidicates the moment immediately before page has been loaded into memory.
Init: Indicates the moment page initializing. InitComplete: Indicates the page has been initialized. PreLoad: Indicates the moment immediately before a page is Loaded. Load: indicates while page Loading. LoadComplete: indicates the moment has been Loaded . PreRender: Indicated the moment immediate before page display in the browser.

PreRenderComplete: Indicates the moment directly before a page has been rendered in
the browser. Unload: Indicates the moment page has been closed. protected void Page_Load(object sender, EventArgs e) { Trace.Write("I am tracing this application"); } protected void page_PreInit(object sender, EventArgs e) { Response.Write("page_PreInit"+"</br>" ); }

Dealing with Postback:


While working with ASP.NET pages, be sure you understand the page events just listed. They are important because you place a lot of your page behavior inside these events at specific points page life cycle.ASP.Net pages typically post back themselves in order to

process events (such as button click event).A post back is just that-a posting back to the
same page. protected void Page_Load(object sender, EventArgs e) { if (Page.IsPostBack == true) { Response.Write("Page Postback"); } else { Response.Write("Page not Postback"); } }

ASP.NET Folders:
App_Code: App_Data: App_Theme:

App_GlobalResources:
App_LocalResources: App_WebRefrences: App_Browsers:

Das könnte Ihnen auch gefallen