Asp. NET pages application (personal grooming) of the way in which values are passed between

Source: Internet
Author: User

 Application
The application variable is valid throughout the application life cycle, similar to using global variables, so it can be accessed from different pages. It differs from the session variable in that the former is a global variable shared by all users, and the latter is a unique global variable for each user.
Examples to explain:
Web site access counter variables are generally used application variable, multiple request access to share this variable, can be manipulated, the variable can be used by the entire application of the various pages directly.
User login account name generally use session variable, multiple requests access to have their own session variable, only to their own session variables to operate, the entire application of the various pages directly use this variable to obtain the user's basic information. (Next article will be organized session)

  Pros : 1. Easy to use, consumes less server resources.

2. Not only can pass the simple data, but also can pass the object.

3. The size of the data volume is unlimited.

  cons : 1. It is easy to be manipulated as a global variable. Therefore, the variables used by individual users are generally not application.

  How to use : 1. Create the name and value you need to pass in the code of the source page construct the application variable: application["Nmae"]= "Value (Or Object)";

2. The code on the destination page uses the application variable to remove the passed value. Result = application["Nmae"]

  Note : The common Lock and unlock methods are used to lock and unlock, in order to prevent concurrent modifications.

  Site Access Example : In the case of not increasing the database field, to count the total number of site visits method, can be processed through the Global.asax file!

(1) Global.asax

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Web.Security;usingSystem.Web.Services.Description;usingSystem.Web.SessionState;namespacewebapplication{ Public classGlobal:System.Web.HttpApplication {protected voidApplication_Start (Objectsender, EventArgs e)            {Application.Lock (); application["Count"] =0;//Application.set ("Count", 0)/application.add ("Count", 0) initializes the variable, which is equivalent to setting count to 0. application["Online"] =0;        Application.UnLock (); }        protected voidSession_Start (Objectsender, EventArgs e)            {Application.Lock (); application["Count"] = (int) application["Count"] +1; application["Online"] = (int) application["Online"] +1;        Application.UnLock (); }        protected voidSession_End (Objectsender, EventArgs e)            {Application.Lock (); Session.Abandon ();//when you end a session, log off the sessionapplication["Online"] = (int) application["Online"] -1;        Application.UnLock (); }    }}

(2) Index.aspx.cs

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;namespacewebapplication{ Public Partial classIndex:System.Web.UI.Page {protected voidPage_Load (Objectsender, EventArgs e) {Response.Write ("<br/> Total visitors are:"+ application["Count"]); Response.Write ("<br/> Current online population is:"+ application["Online"]); }    }}

(3) Web. config (put "<sessionstate mode=" InProc "timeout=" 1 "cookieless=" false "/>" above "</system.web>")

<?xml version="1.0"encoding="Utf-8"?><!--For more information about how to configure an ASP. NET application, go to https://go.microsoft.com/fwlink/?linkid=169433--><configuration> <system.web> <compilation debug="true"targetframework="4.6.1"/> "4.6.1"/> <sessionstate mode="InProc"timeout="1"cookieless="false"/> <!--set a session to a minute, that is, the session will expire without any action within a minute. -</system.web> <system.codedom> <compilers> <compiler language="C#;cs;csharp"extension=". CS"type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, version=1.0.5.0, Culture=neutral, publickeytoken= 31bf3856ad364e35"WarningLevel="4"compileroptions="/langversion:default/nowarn:1659;1699;1701"/> <compiler language="Vb;vbs;visualbasic;vbscript"extension=". vb"type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, version=1.0.5.0, Culture=neutral, publickeytoken= 31bf3856ad364e35"WarningLevel="4"compileroptions="/langversion:default/nowarn:41008/define:_mytype=\&quot; web\&quot; /optioninfer+"/> </compilers> </system.codedom></configuration>

(4) View the effect.

1, run the program in the browser to view, wait a minute after the Refresh page will change;

2, copy the address in the address bar, put in another browser to see the effect;

3. Use the same browser, create a new Incognito window, copy the address in the location bar, can also be viewed.

PS: This article is my reference to the online content plus their own understanding of integration, such as inadvertently violated your rights and interests, please contact me.

  

Asp. NET pages application (personal grooming) of the way in which values are passed between

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.