Application, cookies, stateview, and session usage in Asp.net

Source: Internet
Author: User

Cookie

Cookie usage is similar to ASP. For example, we create a cookie named aspcn with the value of flying knife.

Httpcookie = new httpcookie ["aspcn"];
Cookie. value = "Flying knife ";
Response. appendcookie (cookie );

It's easy to retrieve the cookie value.

Httpcookie cookie = request. Cookies ["aspcn"];
Cookievalue = cookie. value;

Sometimes we want to store multiple pieces of information in a cookie. For example, we add multiple information under the cookie named aspcn.

Httpcookie cookie = new httpcookie ("aspcn ");
Cookie. Values. Add ("webmaster", "Flying knife ");
Cookie. Values. Add ("Writer", "beige ");
Cookie. Values. Add ("linkcolor", "blue ");
Response. appendcookie (cookie );

Retrieving information is as easy as possible

Httpcookie cookie = request. Cookies ["aspcn"];
Value1 = cookies. Values ["webmaster"];
Value2 = cookies. Values ["Writer"];

View state

This is a new concept. Its usage is the same as session. Its main purpose is to record the status of web control. Although it is new, it is no different from the application and session usage, so I don't want to explain it in detail.

State ["droploadindex"] = 0;

The basic usage is as follows. This is useless after it is released, because its purpose is to save the status of the web control.

Application

In ASP. NET, the application usage is the same as that in ASP, and there is almost nothing to say, but it has two more useful events, application_onbeginrequest and application_onendrequest. They are placed in the global file like the original application_onstart and application_onend (note that this file is named Global. Asa in ASP and Global. asax in ASP. NET ).

Note: This event is the same if you do not write on. For example, application_end and application_onend are the same.

Application_onstart is the first event triggered in the entire ASP. NET application, that is, the first ASP. NET event in a virtual directory.ProgramWhen the application is triggered during execution, application_onend is the opposite. It is triggered when the entire application is stopped (usually when the server is restarted/shut down ). Application_onrequeststart and application_onrequestend occur when each ASP. NET program is requested. That is to say, the client accesses an ASP. NET program once, and these two events are triggered. We can see his application from the following program. We should first create a global. asax, the content is as follows:

<Script language = "C #" runat = "server">

Void application_onbeginrequest (Object sender, eventargs E)
{
Response. Write ("request is starting... <br> ");
}

Void application_onendrequest (Object sender, eventargs E)
{
Response. Write ("request is ending... <br> ");
}

</SCRIPT>

Put it in the root directory of the local virtual directory, and then we can open any aspx file.
We are at Global. request is starting... and request is ending... this is not the one we add in this file. We will add another ASP. net file.

Session

Session in ASP. NET is the best process I have ever seen and is unmatched by other dynamic web page technologies. Session in ASP. NET no longer requires the support of cookies. That is to say, if the cookie is disabled, the session value can also be saved.

However, we need to make some preparations for the config. Web file, because we find the session setting text in it, such:

<Sessionstate
Cookieless = "false"
/>

Change cookieless = "false" to cookieless = "true", so the session will not be stored in cookies, but stored in URLs.

In the black circle above, Dongdong is the stored session value. Of course this is encoded and we cannot know its direct real value.

In fact, I found that you do not need to change the config. Web file. After you turn off cookies, you can still pass session values without using URLs. In this case, it is not clear how the session is preserved :)

The session can also remain immortal, that is, when the server is restarted, it can still ensure that the value in the session remains unchanged, but this requires changing the config. Web file. It is also set through the following statement.

<Sessionstate
Inproc = "false"
Server = "localhost"
Port = "42424"
/>
Is it true that the session here is very powerful? :) change localhost to the host you want, and the session can be maintained on another host.

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.