Application 对象用于存储和访问来自任何页面的变量,类似于 session 对象。不同之处在于,所有的用户分享一个 Application 对象,而 session 对象和用户的关系是一一对应的。Application 对象握有会被应用程序中的许多页面使用的信息(比如数据库连接信息)。这意味着可以从任何的页面访问这些信息。同时也意味着你可在一个地点改变这些信息,然后这些改变会自动反映在所有的页面上。而缓存和Application一样是所有用户共享的,但是缓存的生命周期可以根据需要而设定,可以是一秒两秒,也可以是一年两年,前提是这期间应用程序一直在运行,而Application则是存在于应用程序运行期间,当然也可以在程序中干掉它。这两者现在更多的是用缓存。

下面一段简单的MVC代码,用来测试这三者的区别

public class Cache_Session_ApplicationController : Controller
{
public ActionResult Index()
{
return View();
}
public JsonResult SetData(string app,string sess,string cvalue)
{
HttpContext.Application.Lock();
//Application里面的key值是可以重复的
if (null == HttpContext.Application.Get("app"))
HttpContext.Application.Add("app", app);
else
HttpContext.Application.Set("app", app);
HttpContext.Application.UnLock(); Session["sess"] = sess; MemoryCache cache = MemoryCache.Default;
if (null != cache.Get("cache"))
          cache.Remove("cache");
CacheItemPolicy policy = new CacheItemPolicy();
policy.AbsoluteExpiration = DateTime.Now.AddDays();
cache.Add("cache", cvalue, policy);
return new JsonResult() { Data = new { status = "OK" } };
}
public JsonResult GetData()
{
var app= HttpContext.Application.Get("app");
var sess = Session["sess"];
var cache = MemoryCache.Default.Get("cache");
return new JsonResult() { Data=new {app=app??"NULL" , sess = sess??"NULL" , cache = cache??"NULL"} };
}
}
<div>
<input type="button" onclick="SetData()" value="设置值" />
<input type="text" placeholder="application的值" id="zApp"/>
<input type="text" placeholder="session的值" id="zSess" />
<input type="text" placeholder="cache的值" id="zCache" /> <input type="button" onclick="GetData()" value="获取值" />
<label>application的值:</label><label id="appL"></label><br />
<label>session的值:</label><label id="sessL"></label><br />
<label>cache的值:</label><label id="cacheL"></label><br /> </div> <script type="text/javascript">
function GetData() {
$.ajax({
url: "/Cache_Session_Application/GetData",
type: "post",
async: true, //或false,是否异步
success: function (data) {
$("#appL").text(data.app);
$("#sessL").text(data.sess);
$("#cacheL").text(data.cache);
},
error: function () { } });
}
function SetData() {
$.ajax({
url: "/Cache_Session_Application/SetData",
type: "post",
async: true, //或false,是否异步
data: { app: $("#zApp").val(), sess: $("#zSess").val(), cvalue: $("#zCache").val()},
success: function (data) {
alert(data.status);
},
error: function () { } });
}
</script>

asp.net application的更多相关文章

  1. ASP.NET Application Life Cycle

    The table in this topic details the steps performed while an XAF ASP.NET application is running. Not ...

  2. ASP.NET Application and Page Life Cycle

    ASP.NET Application Life Cycle Overview for IIS 7.0 https://msdn.microsoft.com/en-us/library/bb47025 ...

  3. How to increase timeout for your ASP.NET Application ?

    How to increase timeout for your ASP.NET Application ? 原文链接:https://www.techcartnow.com/increase-tim ...

  4. Debug your ASP.NET Application while Hosted on IIS

    转摘:http://www.codeproject.com/Articles/37182/Debug-your-ASP-NET-Application-while-Hosted-on-IIS This ...

  5. ASP.NET Application,Session,Cookie和ViewState等对象用法和区别 (转)

    在ASP.NET中,有很多种保存信息的内置对象,如:Application,Session,Cookie,ViewState和Cache等.下面分别介绍它们的用法和区别. 方法 信息量大小 作用域和保 ...

  6. Custom ASP.NET Application into SharePoint --整合ASP.NET应用程序到SharePoint

    转:http://www.devexpertise.com/2009/02/18/integrating-a-custom-aspnet-application-into-sharepoint-par ...

  7. [转]ASP.net Application 生命周期事件

    生命周期事件和 Global.asax 文件 在应用程序的生命周期期间,应用程序会引发可处理的事件并调用可重写的特定方法.若要处理应用程序事件或方法,可以在应用程序根目录中创建一个名为 Global. ...

  8. php仿照asp实现application缓存的代码分享

    php 怎么没有asp 那样的application缓存呢?最近我找了很多,都只有自己写,下面我分享一段代码 class php_cache{ //相对或者绝对目录,末尾不要加 '/' var $ca ...

  9. Capturing ASP.NET Application Startup Exceptions

    It has become common practice to perform tasks during an ASP.NET applications start up process. Thes ...

  10. asp.net Application、 Session、Cookie、ViewState、Cache、Hidden 的区别

    这些对象都是用来保存信息的,包括用户信息,传递值的信息,全局信息等等.他们之间的区别: 1.Application对象 Application用于保存所有用户的公共的数据信息,如果使用Applicat ...

随机推荐

  1. MVC学习 (一)

    在学习MVC之前对asp.net MVC已经有了一些了解,但是还是有很多的疑问,接下来我慢慢来看书学习并带着问题写博客以作记录. 1.MVC是什么? 2.Asp.net MVC和传统的Asp.net ...

  2. Hadoop 安装(1) CENTOS 安装与配置

    配置虚拟机,名字 Hadoop_Slave4,内存为1024MB,15GB. 进入安装centos. 配置Hostname: Slave4.Hadoop 配置网络,设置静态IP:192.168.1.2 ...

  3. H - Ones

    Description Given any integer 0 <= n <= 10000 not divisible by 2 or 5, some multiple of n is a ...

  4. 配置managed server

    managed server往往是部署应用程序的server,所以最好在weblgoic上配置上managed server,不要把应用程序直接部署到admin server上. 一.受管服务器的创建 ...

  5. JAVA的网络编程【转】

    JAVA的网络编程[转] Posted on 2009-12-03 18:04 火之光 阅读(93441) 评论(20) 编辑 收藏 网络编程 网络编程对于很多的初学者来说,都是很向往的一种编程技能, ...

  6. c++大数模版

    http://blog.csdn.net/hackbuteer1/article/details/6595881 #include<iostream> #include<string ...

  7. swift笔记05

    数组的定义: var 北京十号线 = ["国家图书馆","巴沟"] 北京十号线.count    //或者数组的长度 var a = [Int]() //创建一 ...

  8. 有趣的IT面试题

    一段看起来很简单C代码,预期结果是输出array数组. #include<stdio.h> #define TOTAL_ELEMENTS (sizeof(array) / sizeof(a ...

  9. mysql应用技巧

    1. 查看mysql死锁 SELECT * FROM INFORMATION_SCHEMA.INNODB_TRX; 2.查看正在锁的事务 SELECT * FROM INFORMATION_SCHEM ...

  10. 李维作答 《insideVCL》——李维实在很勤奋,而且勇于突破,从不以旧的内容充数

    (编者按)<Inside VCL(VCL核心架构剖析)>一书出版以来,众多热心读者给李维先生.博文视点公司.CSDN写来信件,有更多朋友在各个论坛上发表关于该书的言论.读者们不但盛赞该书, ...