asp.net unit 01

39
With the increased use of the Internet and advances in information technology, application developers need to quickly create applications that are accessible over the Web or a corporate intranet. The applications should also be efficient and effective. ASP.NET provides developers with various time saving and code saving features. One of its key design goals is to make programming easier and quicker by reducing the amount of code. In addition, it contains several new server controls, which eliminate the need for writing voluminous code. Rationale

Upload: prashanth-shivakumar

Post on 14-Aug-2015

268 views

Category:

Software


1 download

TRANSCRIPT

Page 1: ASP.NET Unit 01

With the increased use of the Internet and advances in information technology, application developers need to quickly create applications that are accessible over the Web or a corporate intranet. The applications should also be efficient and effective.

ASP.NET provides developers with various time saving and code saving features. One of its key design goals is to make programming easier and quicker by reducing the amount of code. In addition, it contains several new server controls, which eliminate the need for writing voluminous code.

Rationale

Page 2: ASP.NET Unit 01

In this session, you will learn to:Identify the basics of Web development

Explore ASP.NET

Develop ASP.NET applications

Objectives

Page 3: ASP.NET Unit 01

Web applications are programs that can be executed on a Web server and accessed from a Web browser.

Web applications enable organizations to share and access information on the Internet and corporate intranets.

A Web application consists of the following types of Web pages:

Static Web page

Dynamic Web page

An application that consists of dynamic Web pages is a dynamic Web application.

A Web application can be made dynamic by using server-side and client-side scripts.

Introducing Web Development

Page 4: ASP.NET Unit 01

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).

Java Server Pages (JSP).

A server-side script is executed on a Web server.

Server-Side Scripting

Page 5: ASP.NET Unit 01

The following figure shows the working of server-side scripts.

Server-Side Scripting (Contd.)

Page 6: ASP.NET Unit 01

Client-side scripting:Enables you to develop Web pages that can dynamically respond to user input without having to interact with a Web server.

Provides dynamic content.

Helps reduce network traffic because it does not need to interact with a Web server to provide dynamic response to user input.

Speeds up the response time of a Web application.

Scripting languages such as VBScript and JavaScript are used to write client-side scripts.

Client-Side Scripting

Page 7: ASP.NET Unit 01

The following figure shows the working of client-side scripts.

Client-Side Scripting (Contd.)

Page 8: ASP.NET Unit 01

Limitations of client-side scripting:Browser support: Client-side scripting is not supported equally by all browsers and operating systems.

Isolation: Client-side scripts cannot access server-side resources.

Security: Client-side scripts are visible to the end users. Therefore, they can be tampered by malicious users.

Thin clients: Web-enabled devices such as mobile phones, palmtop computers, and Personal Digital Assistants (PDAs) do not support client-side scripting such as JavaScript or VBScript.

Client-Side Scripting (Contd.)

Page 9: ASP.NET Unit 01

ASP.NET is a server-side technology that enables programmers to create dynamic Web applications.

ASP.NET has a number of advanced features such as simplicity, security, and scalability that help you develop robust Web applications.

The various advanced features of ASP.NET are based on the .NET Framework.

Exploring ASP.NET

Page 10: ASP.NET Unit 01

The following figure displays the elements of an ASP.NET application and how the elements fit in the broader context of the .NET Framework.

ASP.NET in the .NET Framework

Page 11: ASP.NET Unit 01

The following figure depicts how a Web server processes a request for an ASP.NET file.

Working of an ASP.NET Application

Page 12: ASP.NET Unit 01

An ASP.NET application can be created by using any text editor. However, this is a very time-consuming process.

In addition, 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.

Developing ASP.NET Applications

Page 13: ASP.NET Unit 01

Some of the features of Visual Studio IDE are:Integrated error checking

The Web form designer

An integrated Web server

Developer productivity enhancements

Complete extensibility

Developing ASP.NET Applications (Contd.)

Page 14: ASP.NET Unit 01

Website Types

You can use Visual Studio 2012 to create the following types of websites:

File-system websites

Local Internet Information Services (IIS) websites

Remote IIS websites

FTP sites

Let us see how to create an ASP.NET website

Let us see how to open an existing ASP.NET website

Page 15: ASP.NET Unit 01

The following table lists some specialized directories that an ASP.NET application uses.

Anatomy of an ASP.NET Application

Directory Description

Bin Stores all the compiled .NET components (DLLs) that an ASP. Web application uses.

App_Code Stores source code files that are dynamically compiled to be used in the Web application.

App_Browsers Stores browser definition files. Browser definition files are XML files that define a browser with its properties, capabilities, and features.

App_GlobalResources Stores global resources that are accessible to every page in the Web application.

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.

App_Themes Stores the themes that are used in the Web application.

Page 16: ASP.NET Unit 01

The following table lists some important file types that an ASP.NET Web applications can include.

Anatomy of an ASP.NET Application (Contd.)

File Name DescriptionEnds with .aspx These are ASP.NET Web pages that contain the user interface and, optionally, the

underlying application code. Users request or navigate directly to one of these pages to start a Web application.

Ends with .ascx These are ASP.NET user controls. These controls are similar to Web pages, except that the user cannot access these files directly. Instead, they must be hosted inside an ASP.NET Web page. User controls allow you to develop a small piece of user interface and reuse it in as many Web forms as you want without repeating code.

Ends with .asmx

These are ASP.NET Web services. Web services are collections of methods that can be called over the Internet. Web services work differently than Web pages. However, they still share the same application resources, configuration settings, and memory.

web.config This is the XML-based configuration file for ASP.NET applications. It includes settings for customizing features such as security, state management, and memory management.

Global.asax This is the global application file. You can use this file to define global variables (variables that can be accessed from any Web page in the Web application).

Ends with .cs These are code-behind files that contain C# code. These files allow you to separate the application logic from the user interface of a Web page.

Ends with .ashx These are default HTTP handlers for all Web handlers that do not have a user interface and that include the @ WebHandler directive.

Page 17: ASP.NET Unit 01

Configuration files used in ASP.NET Web applications:ASP.NET uses a hierarchy of configuration files to keep application configuration settings separate from the application code.

A configuration file is an XML file that contains configuration settings for an application and has a .config extension.

Benefits of a configuration file:It provides control and flexibility over the way you run applications.

It eliminates the need to recompile the application every time a setting changes.

It controls access to protected resources, and the location of remote applications and objects by defining configuration settings.

Anatomy of an ASP.NET Application (Contd.)

Page 18: ASP.NET Unit 01

The following table lists the levels, the corresponding configuration files, and their description.

Anatomy of an ASP.NET Application (Contd.)

Level File Name Description

Server Settings Machine.config Present at the root of the configuration hierarchy. It defines global configuration settings for all . Framework applications.

Root Web Settings Web.config Present in the same directory as machine.config. It defines configuration settings for all ASP. applications.

Website settings (optional)

Web.config Present in the root directory of each IIS website. It contains settings that are specific to the website.

Application root Settings (optional)

Web.config Present in the root directory of each application. It contains settings that are specific to the application.

Application subfolder (optional)

Web.config Present in a subfolder of the application root. It contains settings for specific section of a Web application.

Server Settings Machine.config Present at the root of the configuration hierarchy. It defines global configuration settings for all . Framework applications.

Page 19: ASP.NET Unit 01

A Web page is made up of the following two components: The visual portion: It refers to the static HTML and ASP.NET server controls that define the layout of a Web page.

The programming logic: It refers to the code that is required to interact with the Web page.

ASP.NET provides the following two models for managing the visual elements and code:

Single-file page model

Code-behind page model

ASP.NET Web Page Code Model

Page 20: ASP.NET Unit 01

The single-file page model:In the single-file page model, HTML markup of the page and its programming code are in the same physical .aspx file.

The programming code is contained in a <script> block that specifies the attribute runat=“server”.

The single-file page model provides the following advantages:In pages with less code, keeping the programming code and the HTML markup in a single file proves to be very convenient while studying the file.

Pages written by using the single-file model are slightly easier to deploy or to send to another programmer because there is only one file.

A single-file page is easier to rename because there is no dependency between files.

Managing files in a source code control system is slightly easier because the page is self-contained in a single file.

ASP.NET Web Page Code Model (Contd.)

Page 21: ASP.NET Unit 01

The code-behind page model:In the code-behind page model, HTML markup is kept in one file with extension .aspx and the programming code in another file with extension .cs or .vb.

The code file contains a partial class, which indicates that the class contains only some of the total code that makes up the full class for the page.

The code-behind model provides the following advantages: It allows a developer to separate the UI display from the UI processing, thereby enabling the programmer to reuse the programming logic.

It enables a developer to quickly debug the code because the code is not clubbed with the UI.

It enables developers to quickly create a Web application by separating the design and development functions.

ASP.NET Web Page Code Model (Contd.)

Let us see how to add a new Web form to an ASP.NET website

Page 22: ASP.NET Unit 01

All Web forms are instances of the ASP.NET Page class, which is defined in the System.Web.UI namespace.

The Page class inherits the TemplateControl class, which, in turn, inherits the Control class.

ASP.NET uses an event-driven model of programming, which defines a sequence of events that are raised during the lifecycle of a Web page.

The Page Class

Page 23: ASP.NET Unit 01

The following table lists some of the page events:

The Page Class (Contd.)

Event Description

Init It is the first event that occurs when the server executes an ASP.NET page. It occurs only once in the entire lifecycle of an ASP.NET page.

Load It is fired after the Init event, when the page is requested for the first time and whenever the page is reloaded. This event can be used to initialize the variables and the state of the controls that are used in the page.

Unload It represents the last event that occurs in the lifecycle of an ASP.NET page. This event is used to perform final cleanup work, such as the closing of open database connections, discarding objects, or closing files.

Page 24: ASP.NET Unit 01

ASP.NET provides the following built-in objects:Application

Request

Response

Server

Session

HttpContext

The built-in objects are made available to the Page object, which is an instance of the System.Web.UI.Page class from which all ASP.NET pages are inherited.

Intrinsic Objects of ASP.NET

Page 25: ASP.NET Unit 01

The Application object:Provides a reference to an object of the HttpApplicationState class.

Is used to store information that is defined for the entire Web application.

Has the following properties:Count: It returns the number of objects in the HttpApplicationState collection.

Item: It provides access to an object in the HttpApplicationState collection.

Provides the following methods:public void Lock(): It blocks the access to an Application variable or object.

public void UnLock(): It releases the lock procured on an Application variable or object.

Intrinsic Objects of ASP.NET (Contd.)

Page 26: ASP.NET Unit 01

The Request object:Provides a reference to an object of the HttpRequest class.

Enables ASP.NET applications to access information sent by the client during a Web request.

Provides the following method:byte[] BinaryRead(int): Helps retrieve the data sent to the server from the client as part of a post request and stores it in an array.

Intrinsic Objects of ASP.NET (Contd.)

Page 27: ASP.NET Unit 01

The following table describes some of the properties of the Request object.

Intrinsic Objects of ASP.NET (Contd.)

Property Description

ApplicationPath Returns the application root path of ASP.NET on the server. This is a read-only property.

Url Returns a object, which will contain the complete address of the requested page. This is a read-only property.

UserHostAddress Returns the IP host address of the requesting client.

UserLanguage Returns a sorted string array containing the language preferences of the client.

Browser Returns the HttpBrowserCapabilities object, which contains information about the browser capability of the client.

Cookies Returns the HttpCookieCollection object, which provides access to the cookie variables of the client.

QueryString Provides access to the parameters passed from the client to the server along with a request.

Page 28: ASP.NET Unit 01

The Response object:Provides a reference to an object of the HttpResponse class.

Enables ASP.NET applications to send information to the client.

The following table describes some of the properties of the Response object.

Intrinsic Objects of ASP.NET (Contd.)

Property Description

Buffer Sets the buffer feature of the Response object.

Cache Provides access to the caching policy, such as the expiration time and privacy settings of the Web page.

ContentType Sets or gets the type of data that is sent to the client through the Response object.

Cookies Sets a cookie to the client browser.

IsClientConnected

Returns a Boolean value indicating whether the client is connected or disconnected.

Page 29: ASP.NET Unit 01

The following table describes some of the methods of the Response object.

Intrinsic Objects of ASP.NET (Contd.)

Method Description

public void Clear() Clears the content in the buffer of the Response object.

public void Close() Closes the connection to the client.

public void End() Sends the buffered data to the client and closes the connection to the client. The End() method will raise an Application_EndRequest event.

public void Flush() Sends the data in the buffer to the client but does not close the connection to the client.

public void Redirect(string) Redirects the new URL specified in the string parameter to the client.

public void Write(string) Writes a string on the form. It is an overloaded method.

Let us see how to display a message on a Web page

Page 30: ASP.NET Unit 01

The Server object:Provides a reference to an object of the HttpServerUtility class.

Provides methods that can be used to access the methods and properties of the Web server.

Has the following properties:MachineName: It returns the name of the server machine.

ScriptTimeout: It helps get and set the request timeout in seconds for the Web server.

Intrinsic Objects of ASP.NET (Contd.)

Page 31: ASP.NET Unit 01

The following table describes some of the methods of the Server object.

Intrinsic Objects of ASP.NET (Contd.)

Method Description

public void ClearError() Clears the last error that has occurred on the server.

public object CreateObject(String)

Creates the server instance of a COM object.

public Exception GetLastError()

Returns an Exception object, which represents the last error that occurred on the server.

public string MapPath(string path)

Returns the physical file path on the Web server that corresponds to the specified virtual path.

public void Transfer(string)

Terminates the processing of the current page and loads the page specified in the string format.

Page 32: ASP.NET Unit 01

The Session object:Provides a reference to an object of the HttpSessionState class.

Enables ASP.NET applications to keep the state of the application as modified or changed by a client.

Provides access to the session wide cache, which can be used to store information pertaining to the client.

Has the following properties:Count: It returns the number of items in the session-state collection.

SessionID: It returns the unique session ID assigned for a session.

Timeout: It enables to get and set the period in minutes that is allowed between requests by the client before the session-state provider terminates the session.

Intrinsic Objects of ASP.NET (Contd.)

Page 33: ASP.NET Unit 01

Provides the following methods:public void Add(string name, object value): Adds a new item to the session-state collection.

public void Clear(): Clears all the values stored in the session-state collection.

public void Remove(string name): Deletes an item from the session-state collection.

Intrinsic Objects of ASP.NET (Contd.)

Page 34: ASP.NET Unit 01

A new HttpContext object is created for every request sent to an ASP.NET application.

The object is created at the beginning of a request and destroyed when the request is completed.

The HttpContext object holds current request-specific information such as Request, Response, Server, and Session.

Intrinsic Objects of ASP.NET (Contd.)

Page 35: ASP.NET Unit 01

The following table describes some of the properties of the HttpContext object.

Intrinsic Objects of ASP.NET (Contd.)

Property Description

AllErrors Gets an array of errors that are accumulated while processing an HTTP request.

ApplicationInstance Gets or sets the HttpApplication object for the current HTTP request.

Timestamp Gets the initial timestamp of the current HTTP request.

Users Gets or sets security information for the current HTTP request.

Page 36: ASP.NET Unit 01

The following table describes some of the methods of the HttpContext object.

Intrinsic Objects of ASP.NET (Contd.)

Method Description

public void AddError(Exception errorInfo)

Adds an exception to the exception collection for the current HTTP request.

public void ClearError() Clears all errors for the current HTTP request.

public object GetConfig(string name)

Returns requested configuration information for the current HTTP request.

Page 37: ASP.NET Unit 01

In this session, you learned that:Web pages can be of the following types:

Static Web page

Dynamic Web page

Server-side scripting provides users with dynamic content that is based on the information stored at a remote location.

Client-side scripting enables you to develop Web pages that can dynamically respond to user input without having to interact with a Web server.

An ASP.NET application has the following elements:Web Form pages

Configuration files

XML Web service files

Summary

Page 38: ASP.NET Unit 01

ASP.NET has the following advanced features that help develop robust Web applications:

Compiled code

Enriched tool support

Power and flexibility

Simplicity

Manageability

Scalability

Security

Extensibility

A Web form is made up of the following two components: The visual portion

The programming logic

Summary (Contd.)

Page 39: ASP.NET Unit 01

ASP.NET provides two models for managing the visual elements and code:

Single-file page model

Code-behind page model

ASP.NET provides the following built-in objects:Application

Request

Response

Server

Session

HttpContext

Summary (Contd.)