Caching in ASP.NET MVC

简介: The caching options available in ASP.NET MVC applications don’t come from the ASP.NET MVC Framework, but from the core ASP.

The caching options available in ASP.NET MVC applications don’t come from the ASP.NET MVC Framework, but from the core ASP.NET Framework.

 

1. Request-Scoped Caching

Every ASP.NET request begins with the ASP.NET Framework creating a new instance
of the System.Web.HttpContext object to act as the central point of interaction between
components throughout the request.

One of the many properties of the HttpContext is the HttpContext.Items property, a
dictionary that lives throughout the lifetime of the request and which any component
may manipulate.

eg:

 

// how to store data in the collection:
        HttpContext.Items["IsFirstTimeUser"] = true;

        // Retrieving data from the dictionary is just as easy:
        bool IsFirstTimeUser = (bool)HttpContext.Items["IsFirstTimeUser"];


2. User-Scoped Caching

ASP.NET session state allows you to store data that persists between multiple requests.

// store the username in a session
HttpContext.Session["username"] = "Hrusi";

// retrieve and cast the untyped value:
string name = (string)HttpContext.Session["username"];

<system.web>
  <sessionState timeout="30" />
</system.web>

 

3. The ASP.NET Cache

System.Web.Cache is a key/value store

 

4. The Output Cache

ASP.NET provides the ability to operate at a higher level, caching the HTML that is generated as a result of a request.

[OutputCache(Duration=60, VaryByParam="none")]
public ActionResult Contact()
{
    ViewBag.Message = DateTime.Now.ToString();
    return View();
}

 Configuring the cache location

For example, say you want to cache a page that displays the current user’s name. If you
use the default Any setting, the name of the first person to request the page will incorrectly
be displayed to all users.


To avoid this, configure the output cache with the Location property set to Output
CacheLocation.Client and NoStore set to true so that the data is stored only in the user’s
local web browser:

[OutputCache(Duration = 3600, VaryByParam = "none", Location = OutputCacheLocation.Client, NoStore = true)]
public ActionResult About()
{
    ViewBag.Message = "The current user name is " + User.Identity.Name;
    return View();
}

 

 Varying the output cache based on request parameters

For example, say you have a controller action named Details that displays the details
of an auction:

[OutputCache(Duration = int.MaxValue, VaryByParam = "id")]
public ActionResult Details(string id)
{
    var auction = _repository.Find<Auction>(id);
    return View("Details", auction);
}

 

 

目录
相关文章
|
4月前
|
开发框架 前端开发 .NET
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
46 0
|
15天前
|
开发框架 前端开发 JavaScript
JavaScript云LIS系统源码ASP.NET CORE 3.1 MVC + SQLserver + Redis医院实验室信息系统源码 医院云LIS系统源码
实验室信息系统(Laboratory Information System,缩写LIS)是一类用来处理实验室过程信息的软件,云LIS系统围绕临床,云LIS系统将与云HIS系统建立起高度的业务整合,以体现“以病人为中心”的设计理念,优化就诊流程,方便患者就医。
22 0
|
2月前
|
开发框架 前端开发 .NET
进入ASP .net mvc的世界
进入ASP .net mvc的世界
32 0
|
2月前
mvc.net分页查询案例——mvc-paper.css
mvc.net分页查询案例——mvc-paper.css
5 0
|
2月前
|
开发框架 前端开发 .NET
C# .NET面试系列六:ASP.NET MVC
<h2>ASP.NET MVC #### 1. MVC 中的 TempData\ViewBag\ViewData 区别? 在ASP.NET MVC中,TempData、ViewBag 和 ViewData 都是用于在控制器和视图之间传递数据的机制,但它们有一些区别。 <b>TempData:</b> 1、生命周期 ```c# TempData 的生命周期是短暂的,数据只在当前请求和下一次请求之间有效。一旦数据被读取,它就会被标记为已读,下一次请求时就会被清除。 ``` 2、用途 ```c# 主要用于在两个动作之间传递数据,例如在一个动作中设置 TempData,然后在重定向到另
109 5
|
4月前
|
XML 前端开发 定位技术
C#(NET Core3.1 MVC)生成站点地图(sitemap.xml)
C#(NET Core3.1 MVC)生成站点地图(sitemap.xml)
30 0
|
4月前
|
前端开发
.net core mvc获取IP地址和IP所在地(其实是百度的)
.net core mvc获取IP地址和IP所在地(其实是百度的)
128 0
|
4月前
|
前端开发
net core mvc获取IP地址和IP所在地(其实是百度的)
net core mvc获取IP地址和IP所在地(其实是百度的)
19 0
|
6月前
|
开发框架 自然语言处理 前端开发
基于ASP.NET MVC开发的、开源的个人博客系统
基于ASP.NET MVC开发的、开源的个人博客系统
52 0
|
9月前
|
存储 开发框架 前端开发
[回馈]ASP.NET Core MVC开发实战之商城系统(五)
经过一段时间的准备,新的一期【ASP.NET Core MVC开发实战之商城系统】已经开始,在之前的文章中,讲解了商城系统的整体功能设计,页面布局设计,环境搭建,系统配置,及首页【商品类型,banner条,友情链接,降价促销,新品爆款】,商品列表页面,商品详情等功能的开发,今天继续讲解购物车功能开发,仅供学习分享使用,如有不足之处,还请指正。
124 0