ASP. NET Application example of the way to pass values between ASP. NET pages

  • 2021-10-15 10:17:38
  • OfStack

Application

The Application variable is valid throughout the application life cycle, similar to using global variable 1, so it can be accessed in different pages. It differs from Session variables in that the former is a global variable shared by all users, while the latter is a global variable unique to each user.

Give an example to explain:

Web site access counter variable 1 generally adopts Application variable, multiple requests to access when sharing this variable, can be operated on it, the variable can be used directly by the entire application of each page.

User login account name 1 generally uses Session variable, multiple requests have their own Session variable, can only operate on their own Session variable, the entire application of each page directly use this variable to obtain the basic information of users. (Session will be sorted out in the next article)

Advantages: 1. Easy to use and consume less server resources.

2. Not only can you pass simple data, but you can also pass objects.

3. There is no limit to the amount of data.

Disadvantages: 1. As a global variable, it is easy to be misoperated. Therefore, application cannot be used for variable 1 used by a single user.

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

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

Note: lock and unlock methods are commonly used to lock and unlock to prevent concurrent modifications.

Website Visit Example: In the case of not increasing the database field, to statistics of the total number of visits to the website, Global. asax file to deal with!

(1) Global. asax


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.Services.Description;
using System.Web.SessionState;
namespace WebApplication
{
  public class Global : System.Web.HttpApplication
  {
    protected void Application_Start(object sender, EventArgs e)
    {
      Application.Lock();
      Application["count"] = 0; //Application.Set("count",0) /Application.Add("count",0)   Initialize variables, which have the same effect, are all count Set to 0 . 
      Application["online"] = 0;
      Application.UnLock();
    }
    protected void Session_start(object sender, EventArgs e)
    {
      Application.Lock();
      Application["count"] = (int)Application["count"] + 1;
      Application["online"] = (int)Application["online"] + 1;
      Application.UnLock();
    }
    protected void Session_end(object sender, EventArgs e)
    {
      Application.Lock();
      Session.Abandon();// When with 1 Log off the session after the end of the session 
      Application["online"] = (int)Application["online"] - 1;
      Application.UnLock();
    }
  }
}

(2) Index. aspx. cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication
{
  public partial class Index : System.Web.UI.Page
  {
    protected void Page_Load(object sender, EventArgs e)
    {
      Response.Write("<br /> The total number of visitors is: " + Application["count"]);
      Response.Write("<br /> The current number of people online is: " + Application["online"]);
    }
  }
}

(3) Web. config < sessionState mode="InProc" timeout="1" cookieless="false"/ > "Put" < /system.web > "Above)


<?xml version="1.0" encoding="utf-8"?>
<!--
  About how to configure  ASP.NET  For more information about the application, visit 
 https://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
 <system.web>
  <compilation debug="true" targetFramework="4.6.1"/>
  <httpRuntime targetFramework="4.6.1"/>
  <sessionState mode="InProc" timeout="1" cookieless="false"/> <!-- Settings 1 The duration of the session is 1 Minutes, that is 1 If you don't do anything within minutes, the session will fail. -->
 </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) Check the effect.

1. Run the program to view in the browser, and there will be changes after refreshing the page for 1 minute;

2. Put the address in the address bar of Copy in another browser to view the effect;

3. Use the same browser to create a new stealth window. The address in the address bar of Copy can also be viewed.

Summarize


Related articles: