ASP.NET State Management Overview

 

ASP.NET State Management Overview

每次网页被实例化后都要被发送到服务器,在传统的web编程,在这个过程中网页和网页空控件的信息都会丢失。为了克服这种传统编程中的缺点,ASP.net提供了几种方式类保存数据。

View state

Control state

Hidden fields

Cookies

Query strings

Application state

Session state

Profile properties

View state, control state, hidden fields, cookies, query strings 使用多种方式类保存client的数据,然而application state, session state profies properties在服务器的内存中保存数据。每种方式都有各自的优缺点,适用不同情况。

Client-Based State Management Options

View State

Application Life Cycle in General

The following table describes the stages of the ASP.NET application life cycle.

Stage

Description

User requests an application resource from the Web server.

The life cycle of an ASP.NET application starts with a request sent by a browser to the Web server (for ASP.NET applications, typically IIS). ASP.NET is an ISAPI extension under the Web server. When a Web server receives a request, it examines the file-name extension of the requested file, determines which ISAPI extension should handle the request, and then passes the request to the appropriate ISAPI extension. ASP.NET handles file name extensions that have been mapped to it, such as .aspx, .ascx, .ashx, and .asmx.

Note:

If a file name extension has not been mapped to ASP.NET, ASP.NET will not receive the request. This is important to understand for applications that use ASP.NET authentication. For example, because .htm files are typically not mapped to ASP.NET, ASP.NET will not perform authentication or authorization checks on requests for .htm files. Therefore, even if a file contains only static content, if you want ASP.NET to check authentication, create the file using a file name extension mapped to ASP.NET, such as .aspx.

Note:

If you create a custom handler to service a particular file name extension, you must map the extension to ASP.NET in IIS and also register the handler in your application's Web.config file. For more information, see Introduction to HTTP Handlers.

ASP.NET receives the first request for the application.

When ASP.NET receives the first request for any resource in an application, a class named ApplicationManager creates an application domain. Application domains provide isolation between applications for global variables and allow each application to be unloaded separately. Within an application domain, an instance of the class named HostingEnvironment is created, which provides access to information about the application such as the name of the folder where the application is stored.

The following diagram illustrates this relationship:

ASP.NET also compiles the top-level items in the application if required, including application code in the App_Code folder. For more information, see "Compilation Life Cycle" later in this topic.

ASP.NET core objects are created for each request.

After the application domain has been created and the HostingEnvironment object instantiated, ASP.NET creates and initializes core objects such as HttpContext, HttpRequest, and HttpResponse. The HttpContext class contains objects that are specific to the current application request, such as the HttpRequest and HttpResponse objects. The HttpRequest object contains information about the current request, including cookies and browser information. The HttpResponse object contains the response that is sent to the client, including all rendered output and cookies.

An HttpApplication object is assigned to the request

After all core application objects have been initialized, the application is started by creating an instance of the HttpApplication class. If the application has a Global.asax file, ASP.NET instead creates an instance of the Global.asax class that is derived from the HttpApplication class and uses the derived class to represent the application.

Note:

The first time an ASP.NET page or process is requested in an application, a new instance of HttpApplication is created. However, to maximize performance, HttpApplication instances might be reused for multiple requests.

When an instance of HttpApplication is created, any configured modules are also created. For instance, if the application is configured to do so, ASP.NET creates a SessionStateModule module. After all configured modules are created, the HttpApplication class's Init method is called.

The following diagram illustrates this relationship:

The request is processed by the HttpApplication pipeline.

The following events are executed by the HttpApplication class while the request is processed. The events are of particular interest to developers who want to extend the HttpApplication class.

1. Validate the request, which examines the information sent by the browser and determines whether it contains potentially malicious markup. For more information, see ValidateRequest and Script Exploits Overview.

2. Perform URL mapping, if any URLs have been configured in the UrlMappingsSection section of the Web.config file.

3. Raise the BeginRequest event.

4. Raise the AuthenticateRequest event.

5. Raise the PostAuthenticateRequest event.

6. Raise the AuthorizeRequest event.

7. Raise the PostAuthorizeRequest event.

8. Raise the ResolveRequestCache event.

9. Raise the PostResolveRequestCache event.

10.     Based on the file name extension of the requested resource (mapped in the application's configuration file), select a class that implements IHttpHandler to process the request. If the request is for an object (page) derived from the Page class and the page needs to be compiled, ASP.NET compiles the page before creating an instance of it.

11.     Raise the PostMapRequestHandler event.

12.     Raise the AcquireRequestState event.

13.     Raise the PostAcquireRequestState event.

14.     Raise the PreRequestHandlerExecute event.

15.     Call the ProcessRequest method (or the asynchronous version IHttpAsyncHandler.BeginProcessRequest) of the appropriate IHttpHandler class for the request. For example, if the request is for a page, the current page instance handles the request.

16.     Raise the PostRequestHandlerExecute event.

17.     Raise the ReleaseRequestState event.

18.     Raise the PostReleaseRequestState event.

19.     Perform response filtering if the Filter property is defined.

20.     Raise the UpdateRequestCache event.

21.     Raise the PostUpdateRequestCache event.

22.     Raise the EndRequest event.

23.     Raise the PreSendRequestHeaders event.

24.     Raise the PreSendRequestContent event.

Displaying from ASP.NET

有几个方法在ASP.net显示信息。其中一种方法是使用<%=%>,另外一种方法是使用Response.Write

Using <%= %>

Hello<%= name %>!

The Response.Write Statement

<% Response.Write("Hello, World!") %>

<script runat="server" language="JScript">

   function output(str) {

      Response.Write(str);

   }

   var today = new Date();

</script>

Today's date is <% output(today); %>. <BR>

Displaying from a Command-Line Program

command-line 中有三种方法显示数据。

The print Statement

print("Pi is approximately equal to " + Math.PI);

print();

The Console Class

import System;

System.Console.WriteLine("What is your name: ");

var name : String = Console.Readline();

Console.Write("Hello ");

Console.Write(name);

Console.Write("!");

The Show Method

import System.Windows.Forms;

System.Windows.Forms.MessageBox.Show("Welcome! Press OK to continue.");

MessageBox.Show("Great! Now press OK again.");

Displaying Information in the Browser

Using document.write and document.writeln

document.write("Pi is approximately equal to " + Math.PI);

document.write();

Clearing the Current Document

document.clear();

Using Message Boxes

Alert Message Box

window.alert("Welcome! Press OK to continue.");

Confirm Message Box

var truthBeTold = window.confirm("Click OK to continue. Click Cancel to stop.");

if (truthBeTold)

   window.alert("Welcome to our Web page!");

else

   window.alert("Bye for now!");

Prompt Message Box

var theResponse = window.prompt("Welcome?","Enter your name here.");

document.write("Welcome "+theResponse+".<BR>");

posted @ 2008-01-15 16:06  斯伯内德  阅读(520)  评论(0编辑  收藏  举报