Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the four states of ASP.NET?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

What are the four states of ASP.NET? for this question, this article introduces the corresponding analysis and solutions in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.

The principles of these states are very important for .NET development. Now explain it in detail.

View state: the view state is all around you. I don't know if you pay attention to it. ASP.NET is based on the service that handles events. When the server finishes handling the event and returns to this form, if there is no view state, the original data will not exist. You might faint when you say that. For example, there is a website that requires you to register as a member. When you fill in the information (usually the page will go somewhere else) to illustrate the point here, we ask him to return to the original page to continue with the operation, if there is no view state, then the data you filled in before is gone. If there is a view state, the original data is still there. Trying to be in a state is useful sometimes. For example, hierarchical verification.

So in what way does .NET keep trying? Here Microsoft takes a special approach. Open the source code of the web page in the browser and you will find a HTML control that you did not add when editing the web page.

The form is as follows:

This is automatically added to you by. Net. It is also a contributor to the realization of view state. Because this space is hidden, it will not destroy the layout of the page. The values in the value in the empty parts are the data (states) in each empty part and in the control. The data is encrypted by a hash function. It's for security reasons. When a web page is submitted, the browser first saves the various states in the current web page (including the control and the data in the control) to this HTML field. When the page returns to the browser again, these states are automatically returned to the page, so that the page returns to its state.

Let's talk about the problems that need to be paid attention to in the view state.

View state can only be maintained between this page and the server, not when different pages are connected. By default, almost all ASP.NET controls have the ability to maintain view state.

View state has many advantages, but it also has some disadvantages (everything in the world has two sides, and a good technology is no exception). The first is security, although the data is encrypted, but for the ashes of the hackers, it is still very insecure. Third, you can think that if your page contains hundreds of records, the page is very complex, which is bound to affect the loading speed of the page.

Application state (Application): the Application object is the global object of the application. The global shared resources used to hold the application. Its essence is an instance of the HttpApplicationState class. It is created when the user asks a resource in a virtual directory for the first time. In other words, when the website is published on the Internet * * times, and a user happens to access your virtual directory resources, then the Application object has been created. A buffer is established between the resource and the application.

The application status exists only when the Web site is running. When the application is rolled out or the server is shut down (worse, the server crashes), the data saved in the application state can be lost and corrupted. Therefore, for resources that need to be retained by * *, it is better to persist in the database.

Introduces some manipulation methods of some Application objects.

Application objects are, of course, defined using the most classic dictionary method of key-value pairs. Where the key is a string representing the name of the Application object. The value can be any type of data. (for example, HTML,CSS,SQL is fine). 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 "); take a harder operation and invite all the objects in the Application out. Application.Clear (); or Appication.RemoveAll (); information sharing naturally leads to a problem, synchronous competition for resources. Of course, the solution to this kind of problem depends on the lock. Applicatin.Lock () / / locks the resource so that it cannot be accessed by other processes. Functions (Application ["source"]); / / some operations that manipulate Application objects. Application.UnLock (); / / unlock.

Session state (Session): the Session object is used to hold the state of a single user. In a Web site, each newly visited user will generate its own session (Session) object. This Session object is managed on the server side and can only be served by the users to which it is bound. If another user also visits the site, he also has his own Sesiion object. Two users' Session objects cannot share the same Session object even if they have the same name. Each is his own.

The Session object is actually an instance of the HttpSessionState class. There are many properties and methods that you might as well take a look at. I won't elaborate here.

Just some examples.

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

Application status is always available on the website, and we don't care about the internal mechanism of. NET Framework. Let's look at the Session object. Because the Session setting in the Machine.config configuration file is started, no additional steps are required to use it. Nevertheless, we still need to know some principles and essence. The settings in the Machine.config and the Web.config of the application determine whether to start or close the Session object. Of course, if you want to delay it until you need it, you can enable it. We can set it at the page level.

The purpose of this statement is that session state (Session) cannot be used on this page.

The Session object has a Timeout property that sets the life cycle of the Session object. It is measured in minutes, and the default is 20 minutes. If the server is not connected within the valid time, all settings of Session will be invalidated. If you need to terminate the Session object, you can use its Abandon () method.

Almost forgot, Session has a very important point, add.

Keywords in session are case-insensitive. So don't distinguish Session variables by case. In other words, Session ["source"] is the same as Session ["SOURCE"]. If you delete one of them, there will be unexpected results.

Cookie status: Coolkie status one is used to save the state of a user resource. Unlike Session, it is stored on the browser side. Cookie can only contain less information, usually no more than 4096 bytes (some newer browsers can reach 8192 I bytes). In ASP.NET, Session objects and Cookie objects are now combined to identify users. Whenever the user starts to connect to the site, the system will automatically create a Session object related to the user in the memory block, and at the same time create a Cookie object to save the user ID and save it on the browser side, connecting with the current user. In this way, the next time the user visits, the user is required to submit the user's TD, that is, CooKie,Cookie and Session, to correctly restore the original session state. This is the way to keep the user state under the condition of stateless protocol Http.

The way to write Cookie to the browser is like this. For example:

HttpCookie cookie=new HttpCookie ("userNameId"); / / creates a Cookie object and assigns a value. Cookie.Value= "userNameId" / / if a cookie already exists, you can assign it the same way. Cookie.Expires=DateTime.Now+TimeSpan;// sets the life cycle of cookie, where TimeSpan is an instance of the TimeSpan class. The default time for cookie is 20 minutes. Response.Cookies.Add (cookie); / / write Cookie.HttpCookie cookie1=Request.Cookies ["cookie"] to the browser; / / read Cookie. The answers to the questions about the four states of ASP.NET are shared here. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report