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. Linux shell中的一个问题 ${}带正则匹配的表达式

    目前在准备龙芯项目的PMON,在研究其编译过程的时候,看到一些make 语句,百思不得其解.后来在shell编程中看到一点资料,牵扯到Shell中的正则表达式.故记录下来,以备后来查阅. 问题: 在某 ...

  2. 【熊猫】POS销售

    select a.itemcode,b.itemname,b.spec,b.unit,b.rprice,sum(a.rqty) rqtyfrom tm_possale_detail a,sys_ite ...

  3. HtmlHelp

    @Html.Label("Label", "Label") @*渲染成<label for="Label">Label</ ...

  4. 数据库监控[Z]

    --查看表锁 select * from sys.v_$sqlarea where disk_reads>100    --监控事例的等待 select event,sum(decode(wai ...

  5. Entity Framework排序

    public ActionResult Index() { using (ApplicationDbContext db = new ApplicationDbContext()) { //var l ...

  6. css基础之 语法

    CSS 实例 CSS 规则由两个主要的部分构成:选择器,以及一条或多条声明: 选择器通常是您需要改变样式的 HTML 元素. 每条声明由一个属性和一个值组成. 属性(property)是您希望设置的样 ...

  7. HDU 2072(单词数)题解

    以防万一,题目原文和链接均附在文末.那么先是题目分析: [一句话题意] “就是统计一篇文章里不同单词的总数”(已经是一句话了..) [题目分析] 明显需要去重,上set,因为按行分析,又没有EOLN用 ...

  8. js 打开新页面 window.open()

    利用js打开一个新页面,而不是一个新窗口. 在网上各种东西啊,蛋疼了半天,还白疼了.. 后来看到a标签有target属性,然后又发现window.open()的第二个参数是target,然后我笑了(e ...

  9. HTML5数组方法

    ***************************** forEach方法 *****************************   <script>var data = [1, ...

  10. 七牛云Fetch第三方资源并转码(PHP版)

    七牛云的图片加速一直在用,好用没得说,最近项目需要做个微信端录音,然后上传,别人试听的功能,录音和上传用的都是微信的接口,有文档,比较方便,但是上传后,微信只给保存3天,所以就下载到了七牛,也就用到了 ...