Such an application can be created by using server-side scripts, client-side scripts, or both in addition to HTML pages.. When a browser requests for information on a website that is cre
Trang 1Introducing ASP.NET C hap
Web applications have revolutionized the way
business is conducted These applications enable
organizations to share and access information from
anywhere and at anytime This has majorly moved the focus of application development from desktop
applications to Web applications One of the most
popular server-side technologies used today for
developing Web applications is ASP.NET
This chapter introduces the basics of Web
development It also discusses the architecture,
functioning, and features of ASP.NET In addition, it
explains how to develop ASP.NET applications
In this chapter, you will learn to:
Identify the basics of Web development
Explore ASP.NET
Develop ASP.NET applications
Objectives
Trang 3Among all technologies, the Internet has been the fastest growing technology Ever since its inception, the Internet has evolved exponentially In the recent years, it has changed the way business is conducted
Prior to the evolution of the Internet, organizations were unable to create awareness about themselves because the communication media were limited Moreover, the organizations could deliver only limited information by using the existing communication media such
as television and radio However, with the inception of the Internet, organizations found a new medium through which they could reach a larger range of people irrespective of their geographical locations Therefore, organizations increasingly became dependent on the Internet for sharing and accessing information This resulted in changing the focus of application development from desktop applications to Web applications
Web applications are programs that are executed on a Web server and accessed from a Web browser These applications enable organizations to share and access information on the Internet and corporate intranets This information can be accessed from anywhere and
at any time In addition, Web applications can support online commercial transactions popularly known as e-commerce An online store accessed through a Web browser is an example of a Web application
A Web application consists of Web pages Web pages can be of the following types:
Static Web page: A Web page that consists of only Hyper Text Markup Language
(HTML) is a static Web page A static Web page does not respond dynamically to the actions performed by a user
Dynamic Web page: A Web page that responds dynamically to the actions
performed by a user is a dynamic Web page and an application that consists of dynamic Web pages is a dynamic Web application Such an application can be created by using server-side scripts, client-side scripts, or both in addition to HTML pages
With the increase in the usage of the Internet as the information medium, organizations are creating dynamic Web applications Dynamic Web applications enable a user to:
Retrieve data from a database that may not reside on the computer where the Web server is located
Submit data from HTML forms
Get information based on the input
Introducing Web Development
Server-Side Scripting
Trang 4To incorporate these features in a Web application, you need to implement server-side scripting Server-side scripting provides users with dynamic content that is based on the information stored at a remote location, such as a back-end database Server-side scripting includes code written in server-side scripting languages, such as Active Server Pages
(ASP) and Java Server Pages (JSP) A server-side script is executed on the Web server When a browser requests for information on a website that is created by using server-side technology, the Web server to which the request is sent, first processes the script and then sends the results back to the browser For example, if a Web page includes a server-side script to display the current time of the system on which the website is hosted, the script will be processed at the server and the current system time will be sent back to the
browser
The following figure shows the working of server-side scripts
Working of Server-Side Scripts
Client-side scripting enables you to develop Web pages that can dynamically respond to user input without having to interact with a Web server Suppose, you have a Web
application that requires users to enter the user name and password before displaying the home page You need to ensure that a user does not leave the user name and password fields blank To check whether a user has left the user name and password fields blank, you can write client-side scripts
Client-Side Scripting
Trang 5In addition to providing dynamic content, a client-side script helps reduce network traffic because it does not need to interact with a Web server to provide dynamic response to the user input Client-side scripting also speeds up the response time of a Web application This happens because a Web server is not overloaded with the job of processing the
client-side script for every client Scripting languages, such as VBScript and JavaScript, are used to write client-side scripts
The following figure shows the working of client-side scripts
Working of Client-Side Scripts
Limitations of Client-Side Scripting
In spite of the all the advantages, client-side scripting has some major limitations These limitations are:
Browser support: Client-side scripting is not supported equally by all browsers and
operating systems
Isolation: Client-side scripts cannot access server-side resources It is difficult and
requires a lot of coding to access a file or interact with a database on the server by using client-side scripts
Security: Client-side scripts are visible to the end users Therefore, they can be
tampered by malicious users
Trang 6Note
Thin clients: Web-enabled devices such as mobile phones, palmtop computers, and
Personal Digital Assistants (PDAs) can communicate with Web servers These devices act as thin clients that can use Web applications containing server-side scripting However, these devices do not support client-side scripting such as JavaScript or VBScript
In spite of all the limitations, client-side scripting is still in use ASP.NET allows you to use client-side scripting in addition to the server-side scripting to create rich and
responsive Web applications
Trang 7ASP.NET is a server-side technology that enables programmers to create dynamic Web applications It has a number of advanced features such as simplicity, security, and
scalability that help you develop robust Web applications These advanced features of ASP.NET are based on the NET Framework
ASP.NET is built on the Microsoft NET Framework Microsoft introduced the NET Framework to help developers create globally distributed software with Internet
functionality and interoperability
The following figure displays the elements of an ASP.NET application and how the
elements fit in the broader context of the NET Framework
Elements of an ASP.NET Application in the NET Framework
The preceding figure indicates that an ASP.NET application has the following elements:
Web Form pages: Enable you to include user interfaces, such as Text box control,
List box control, and the application logic of Web applications
Exploring ASP.NET
ASP.NET in the NET Framework
Trang 8 Configuration files: Enable you to store the configuration settings of an ASP.NET
application
XML Web Service files: Define a Web service that can be called by an ASPX file
over the Internet or from a local network
Web forms and the state management feature of ASP.NET constitute the ASP.NET Page Framework There are also a number of runtime services available in ASP.NET The ASP.NET runtime services include view state, session state, and application state
management, Web security, and the caching mechanism of ASP.NET applications The runtime services of ASP.NET interact with NET Framework base classes, which in turn, interact with the Common Language Runtime (CLR) to provide a robust Web-based development environment
After creating an ASP.NET application, the ASP.NET files need to be stored on a Web server such as Internet Information Services (IIS) server, which is the Web server for the Windows platform The Web server processes the ASP.NET files and then executes the scripts Finally, the results are sent to the Web browser that requested the ASP.NET file The following figure depicts how a Web server processes a request for an ASP.NET file
Processing of a Request for an ASP.NET File
Working of an ASP.NET Application
Trang 9The steps involved in the execution of an ASP.NET file are:
1 A Web browser sends a request for an ASP.NET file to a Web server by using a Uniform Resource Locator (URL)
2 The Web server, such as IIS, receives the request and retrieves the appropriate
ASP.NET file from the disk or memory
3 The Web server forwards the ASP.NET file to the ASP.NET script engine for
processing
4 The ASP.NET script engine reads the file and executes any server-side script it encounters
5 The processed ASP.NET file is generated as an HTML page
6 The Web server then sends the HTML page to the client
7 The Web browser interprets the output and displays it
A Web server generates and sends only the HTML output to the client As a result, it helps to hide the code of the ASP.NET file from the users who access an ASP.NET Web page
ASP.NET script engine is the engine that processes the server-side scripts contained in
an ASP.NET application After processing the scripts, the engine renders the result as
an HTML document and sends it back to the browser
In addition to hiding the code of the ASP.NET file from the users, ASP.NET has the following advanced features that help develop robust Web applications:
Compiled code: The code written in ASP.NET is compiled and not interpreted This
makes ASP.NET applications faster to execute than other server-side scripts, such as the scripts written in a previous version of ASP that are interpreted
Power and flexibility: ASP.NET applications are based on CLR As a result, the
power and flexibility of the NET platform is available to ASP.NET applications ASP.NET applications enable you to ensure that the NET Framework class library, messaging, and data access solutions are seamlessly accessible on the Web ASP.NET is also language-independent As a result, you can choose any NET language to develop your application
Features of ASP.NET
Trang 10 Simplicity: ASP.NET enables you to build user interfaces that separate application
logic from the presentation content In addition, CLR simplifies application development by using managed code services, such as automatic reference counting and garbage collection Therefore, ASP.NET makes it easy to perform common tasks ranging from form submission and client authentication to website configuration and deployment
Manageability: ASP.NET enables you to manage Web applications by storing the
configuration information in an Extensible Markup Language (XML) file You can open the XML file and modify it
Scalability: ASP.NET has been designed with scalability in mind It has features that
help improve performance in a multiprocessor environment
Security: ASP.NET provides a number of options for implementing security and
restricting user access to a Web application All these options are configured within the configuration file
Extensibility: ASP.NET provides a number of options to create an extensible Web
application For example, you can create custom controls and extend the default Hypertext Transfer Protocol (HTTP) pipeline The path through which the data flows from the client to the server is called the HTTP pipeline It is a technique in which multiple HTTP requests are written out to a single socket without waiting for the corresponding responses It improves the page loading time
Trang 11You can create ASP.NET applications by using any text editor However, this will take a substantial amount of time because you will have to create the applications by creating all the Web page classes and coding the Web page controls manually In addition to being time-consuming, this process increases the possibility of inducing errors that could
otherwise be avoided by using a design tool A design and productivity tool, such as Visual Studio, provides various features that enable the developers to quickly create
robust ASP.NET applications Some of the Visual Studio IDE features are:
Integrated error checking: Visual Studio can detect various problems such as data
type conversion errors, missing namespaces or classes, and undefined variables When you type code, Visual Studio detects and underlines these problems
The Web form designer: Visual Studio provides the Web form designer that
enables you to create a Web page by simply dragging the ASP.NET controls to the appropriate location and configuring their properties The actual Web page markup is created automatically
An integrated Web server: Web applications need to be hosted on a Web server
such as IIS to be accessible for the users Therefore, you need to install IIS on the development computer to test the applications However, if you are creating a Web application by using Visual Studio, you need not install IIS on the computer This is because Visual Studio has an integrated development Web server that enables you to run a Web application directly from the design environment
Developer productivity enhancements: Visual Studio provides features such as
automatic statement completion and color-coded syntax that makes coding quick and efficient In addition, Visual Studio has an integrated debugger that allows you to watch code execution, pause your program at any point, and inspect the contents of any variable This helps in debugging the code quickly and efficiently
Complete extensibility: Visual Studio allows you to use macros, change project
templates, and even add your own custom add-ins This provides you to extend the existing functionality of the existing environment
You can use ASP.NET to create websites and Web services A website is the user
interface of your application Generally, a website represents a web application front-end, with screens, Cascading Style sheets (CSS), and User Interface (UI) components A Web service enables you to remotely access certain functions at the server side It enables the exchange of data in a client-to-server or a server-to-server environment over the Web by using the Simple Object Access Protocol (SOAP) A Web service does not have a user interface
Developing ASP.NET Applications
Creating an ASP.NET Web Application
Trang 12Task 1.1: Creating an ASP.NET Website
Task 1.2: Opening an Existing ASP.NET Website
ASP.NET applications are generally divided into multiple Web pages Every Web page in
an ASP.NET application shares a common set of resources and configuration settings These resources and configuration settings cannot be shared by the Web pages from any other ASP.NET application, even if they are on the same Web server
Every ASP.NET application is executed inside a separate application domain These application domains ensure that even if a Web application causes a fatal error, it does not affect other applications that are currently running on the same computer Application domains also restrict a Web page in one application from accessing the in-memory
information of another application Each Web application is a separate entity that has its own set of data It can be described as a combination of files, pages, handlers, modules, and executable code that can be invoked from a virtual directory on a Web server
Anatomy of an ASP.NET Application
Trang 13The following figure shows a Web server that is hosting four Web applications
A Web Server Hosting Four Web Applications
A virtual directory is a directory that is exposed to the public on a Web server
Trang 14Directories Used in ASP.NET Web Applications
An ASP.NET application uses some specialized directories The following table lists these directories with their descriptions
Directory Description
Bin Stores all the compiled NET components (DLLs) that an ASP.NET Web
application uses For example, if you create a custom component, you need to place the component in this directory Once placed in the Bin directory, the component can be used by any ASP.NET application App_Code Stores source code files that are dynamically compiled to be used in the
Web application It is similar to the Bin directory except for one difference It contains source code files whereas the Bin directory contains compiled assemblies
App_Browsers Stores browser definition files Browser definition files are XML files
that define a browser with its properties, capabilities, and features This information is used by ASP.NET server controls to determine the browser that has requested the page and render the appropriate markup for that browser
App_GlobalResources Stores global resources that are accessible to every page in the Web
application It is used for localization purposes where you want a website to display content in more than one language
App_LocalResources Stores resx files that are accessible to a specific page only
App_WebReferences Stores references to Web services that are used by the Web application App_Data Stores data such as database files and XML files that are used by the
application It is not mandatory to store data files in this directory App_Themes Stores the themes that are used in the Web application
Directories Used in an ASP.NET Application
Some of the subdirectories given in the preceding table are created in specific
applications For example, the App_Themes directory is created when you use themes in your application