This article mainly introduces you to the usage of Application in ASP.NET C#. Before introducing the usage of Application, I first introduce the usage of Session for your reference and study. The introduction in the article is very detailed. Friends who need it Let’s follow the editor to learn together.
Application object
The Application object lifetime is as long as the Web application lifetime. The lifetime is determined from the Web application web page. When the access starts, the HttpApplication class object Application is automatically created. When no web page is accessed, the Application object is automatically revoked. Therefore, the variables in the Application object also have the same lifetime, and the variables can be accessed by all web pages in the Web application. Therefore, some global public variables can be established in the Application object. Since the values stored in the Application object can be read by all web pages of the application, the properties of the Application object are also suitable for transferring information between web pages of the application.
Application object mainly has the following purposes:
l Store and record the number of people online or the total number of people visiting the website Variables.
l Stores the latest news shared by the website for all web pages to update.
l Record the number of times or time the same advertisement is clicked on each web page of the website.
l Stores database data used by all web pages.
l Communication between different applications, such as multi-user chat rooms, multi-user games, etc.
About ASP.NET Application The usage is very different from Session. Let’s take a look at the detailed introduction:
Usage of Session
1. When the Session.Add name is the same, it will not be repeated. , but override.
Session.Add("s1", 1); Session.Add("s1", 2); // s1 最终只有一个值,就是 2。
2. Names ignore case.
Session.Add("s1", 1); Response.Write(Session["S1"]); // 值为 1
3. The value can be obtained immediately after Session Add (the same is true for Remove). This is different from Cookie, which has to wait until the next page. Only then.
Session.Add("s1", 1); Response.Write(Session["s1"] == null); // False,它不为 null
4. The stored Session data type is object, and it is best to use Convert for conversion.
Convert.ToInt32(Session["s1"]);
If you convert to string, it is best to use Convert.ToString() instead of Session["s1"].ToString(), because if Session is null , an error will be reported after using the method.
5. Use Session in the class.
System.Web.HttpContext.Current.Session
Usage of Application
Duplicate name problem
HttpContext.Current.Application.Add("key1", "value1"); HttpContext.Current.Application.Add("key2", "value2"); HttpContext.Current.Application.Add("KEY2", "value3"); // name 忽略大小写 int count = HttpContext.Current.Application.Count; // 3 个 string[] keys = return HttpContext.Current.Application.AllKeys; // key1、key2、key2 string s = (string)HttpContext.Current.Application.Get("key2"); // value2 string s2 = (string)HttpContext.Current.Application.Get(2); // value3
The code is as above, and the results are listed in the remarks. It can be seen that when Application encounters the same key value, it neither reports an error nor overwrites the previous one, but exists at the same time. When you use the key name to get the value, you get the first corresponding value with the same name. If you have to get the last one, use index.
If we want to overwrite the same name, we can use the following code
HttpContext.Current.Application.Add("key1", "value1"); // HttpContext.Current.Application.Add("key2", "value2"); string name = "key2"; object obj = HttpContext.Current.Application.Get(name); if (obj == null) { // 不存在,直接添加 HttpContext.Current.Application.Add(name, "value2"); } else { // 存在,不能直接调用 Add 方法,这样会造成两个相同 name 的条目 // obj = "value3"; // 这种方法行不通 HttpContext.Current.Application[name] = "value3"; } return (string)HttpContext.Current.Application[name]; // 用 [] 取值时,等同于 Get 方法
In the above code, directly modifying obj will not work, but when If you want to get the object, the following code will work. Note: This is a knowledge point about C# value reference and address reference, and has nothing to do with Application.
((Site)obj).Url = "222"; // 行得通
The above is the detailed content of Tutorial on how to use Application in ASP.NET C#. For more information, please follow other related articles on the PHP Chinese website!

The combination of C# and .NET provides developers with a powerful programming environment. 1) C# supports polymorphism and asynchronous programming, 2) .NET provides cross-platform capabilities and concurrent processing mechanisms, which makes them widely used in desktop, web and mobile application development.

.NETFramework is a software framework, and C# is a programming language. 1..NETFramework provides libraries and services, supporting desktop, web and mobile application development. 2.C# is designed for .NETFramework and supports modern programming functions. 3..NETFramework manages code execution through CLR, and the C# code is compiled into IL and runs by CLR. 4. Use .NETFramework to quickly develop applications, and C# provides advanced functions such as LINQ. 5. Common errors include type conversion and asynchronous programming deadlocks. VisualStudio tools are required for debugging.

C# is a modern, object-oriented programming language developed by Microsoft, and .NET is a development framework provided by Microsoft. C# combines the performance of C and the simplicity of Java, and is suitable for building various applications. The .NET framework supports multiple languages, provides garbage collection mechanisms, and simplifies memory management.

C# and .NET runtime work closely together to empower developers to efficient, powerful and cross-platform development capabilities. 1) C# is a type-safe and object-oriented programming language designed to integrate seamlessly with the .NET framework. 2) The .NET runtime manages the execution of C# code, provides garbage collection, type safety and other services, and ensures efficient and cross-platform operation.

To start C#.NET development, you need to: 1. Understand the basic knowledge of C# and the core concepts of the .NET framework; 2. Master the basic concepts of variables, data types, control structures, functions and classes; 3. Learn advanced features of C#, such as LINQ and asynchronous programming; 4. Be familiar with debugging techniques and performance optimization methods for common errors. With these steps, you can gradually penetrate the world of C#.NET and write efficient applications.

The relationship between C# and .NET is inseparable, but they are not the same thing. C# is a programming language, while .NET is a development platform. C# is used to write code, compile into .NET's intermediate language (IL), and executed by the .NET runtime (CLR).

C#.NET is still important because it provides powerful tools and libraries that support multiple application development. 1) C# combines .NET framework to make development efficient and convenient. 2) C#'s type safety and garbage collection mechanism enhance its advantages. 3) .NET provides a cross-platform running environment and rich APIs, improving development flexibility.

C#.NETisversatileforbothwebanddesktopdevelopment.1)Forweb,useASP.NETfordynamicapplications.2)Fordesktop,employWindowsFormsorWPFforrichinterfaces.3)UseXamarinforcross-platformdevelopment,enablingcodesharingacrossWindows,macOS,Linux,andmobiledevices.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

Atom editor mac version download
The most popular open source editor

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software