Describes the four statuses of ASP. NET.

Source: Internet
Author: User
Tags key string

The principles of these States are very important for. NET development. Now let's explain in detail.

View status:The view status is around and you do not know whether to pay attention to it. ASP. NET processes events based on services. When the server returns this form after processing the event, if there is no view status, the original data will not exist. In this case, it may be dizzy. For example, a website requires you to register as a member. When you fill in the information, the page will generally turn to another place) here to illustrate the problem, we asked him to return the original page to continue the operation, if there is no view status, the data you filled out earlier is no longer available. If the view status exists, the original data is still present. The attempt status is useful in some cases. For example, in hierarchical verification.

So what method does. NET use to keep the attempt status? Here, Microsoft adopts a special method. Open the source code of the webpage in a browser and you will find an HTML control that you did not add when editing the webpage.

The format is as follows:

<Input type = "hidden" name = "_ VIEWSTATE" id = "_ VIEWSTATE" value = "some encrypted values"/>

This is automatically added to you by. NET. It is also the hero of view status implementation. Because the space is hidden, the page layout will not be damaged. The value in the null value is the data status of each empty part and the control ). The data is encrypted by the hash function. This is for security considerations. When a webpage is submitted, the browser first saves various states in the current webpage, including controls and data in controls, to this HTML field. When the web page returns to the browser again, the status is automatically returned to the web page, so that the web page is restored.

Let's talk about the issues that need to be paid attention to in view status.

The view status can only be maintained between the current webpage and the server, and cannot be maintained when different webpages are connected. By default, almost all ASP. NET controls maintain the view State.

View status has many advantages, but everything in the world has two sides, and a good technology is not listed. The first is security. Although the data is encrypted, it is still insecure for hardcore characters in hackers. Again, you can think that if your page contains hundreds of records, the page is very complicated, which will affect the loading speed of the page.

Application Status Application ):The Application object is the Global Object of the Application. Used to store global shared resources of applications. Its essence is an instance of the HttpApplicationState class. When the user first asks a resource in a virtual directory, it is created. That is to say, the website was released on the Internet for the first time. When a user accesses your virtual directory resources, the Application object has been created. A buffer zone is created between resources and applications.

The application status only exists when the website is running. When an application is launched or the server is shut down, the server crashes.) data stored in the application status is lost and damaged. Therefore, resources that need to be permanently retained are stored persistently in the database.

Describes some operation methods of Application objects.

Of course, the Application object is defined using the dictionary method of the most classic "key-value" pair. The middle-key string represents the name of the Application object. The value can be any type of data. Such as HTML, CSS, and SQL ). For example:

Application ["source"] = "special source ";

String message = Application ["source"]. ToString ();

Add and delete saved objects in Application.

Application. Add ("source", ''special source ");

Application. remove ("source ");

In this case, all the objects in the Application are invited out.

Application. Clear ();

Or Appication. RemoveAll ();

Information sharing naturally leads to a problem where resources are competing simultaneously. Of course, the lock is needed to solve these problems.

Applicatin. Lock () // Lock the resource to prevent other processes from accessing it.

Functions (Application ["source"]); // some operations on the Application object.

Application. UnLock (); // UnLock.

Session status (Session ):The Session object is used to save the status of a single user. On the website, each new user will generate his/her own Session) object. This Session object is managed on the server and can only serve the user it is bound. If another user accesses the website, he also has his own Sesiion object. The Session objects of two users cannot share the same Session object even if they have the same name. Each is different.

In fact, the Session object is an instance of the HttpSessionState class. There are many attributes and methods available for you to take a look. I will not discuss it here.

Here are some examples.

Session["source"]="special source";

string message =Session["source"].ToString();

The application status is always available on the website. We don't care about the internal mechanism of the. NET Framework. Let's take a look at the Session object. Since the Session settings in the Machine. config configuration file are started, you can use the settings without any additional steps. Even so, we still need to know some principles and essence. The settings in Machine. config and Web. config of the application determine whether to start or close the Session object. Of course, if you want to delay to the desired time, you can also enable it. We can set it at the page level.

<% @ Page Language = "C #" EnableSessinState = "False" %> // This statement does not support Session state sessions on this Page ).

The Session object has a Timeout attribute to set the lifecycle of the Session object. It is measured in minutes. The default value is 20 minutes. If the server is not connected within the validity period, all Session settings will be invalid. To terminate a Session object, use its Abandon () method.

I almost forgot. The Session is very important. Just add it.

The keywords in the session are case-insensitive. Therefore, do not use case sensitivity to distinguish Session variables. That is to say, Session ["source"] And Session ["SOURCE"] are the same. If one of them is deleted, unexpected results will occur.

Cookie status:Coolkie status 1 is used to save the status of a user resource. Unlike Session, it is stored on the browser side. The Cookie can contain only a small amount of information, usually no more than 4096 bytes (some newer browsers can reach 8192 I bytes ). in ASP. NET, Session objects and Cookie objects are used to identify users. Whenever a user starts to connect to the site, the system will automatically create a Session object related to the user in the memory block, and create a Cookie object to save the user ID and save it on the browser side, it is uniquely associated with the current user. In this way, when the user accesses the next time, the user is requested to submit the user's TD, that is, the CooKie. The Cookie and Session are compared to correct restoration of the original Session status. This is how to maintain the user status under the stateless Http protocol.

The method for writing cookies to a browser is as follows. For example:

HttpCookie = new HttpCookie ("userNameId"); // create a cookie object and assign values.

Cookie. Value = "userNameId" // you can assign a Value to a Cookie if it already exists.

Cookie. Expires = DateTime. Now + TimeSpan; // set the cookie lifecycle,

Here, TimeSpan is an instance of the TimeSpan class. The default cookie time is 20 minutes.

Response. Cookies. Add (cookie); // write the Cookie to the browser.

HttpCookie cookie1 = Request. cookie ["Cookie"]; // read cookie.

  1. ASP. NET design ideas
  2. Video tutorial: ASP. NET Web development
  3. Basic ASP. net mvc knowledge

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.