ASP.NET MVC缓存使用

 

局部缓存(Partial Page)

1.新建局部缓存控制器:html

    public class PartialCacheController : Controller
    {
        // GET: /PartialCache/
        [OutputCache(Duration = 5)] //缓存5秒
        public ActionResult Index()
        {
            ViewBag.Time = DateTime.Now;
            return PartialView(); //这里返回的是分部视图
        }
    }

本想将缓存配置放到配置文件里面,但报错了:web

        [OutputCache(CacheProfile = "CacheTime")]
        public ActionResult Index()
        {
            ViewBag.Time = DateTime.Now;
            return PartialView();
        }

配置文件system.web节点下:缓存

    <caching>
      <outputCacheSettings>
        <outputCacheProfiles>
          <add name="CacheTime" duration="5" varyByParam="none"/>
        </outputCacheProfiles>
      </outputCacheSettings>
    </caching>

运行报错:spa

这是个Bug问题,.net

解决方法参考:csdn123.com/html/itweb/20130906/104315_104295_104338.htm3d

 感受比较麻烦,分部视图缓存仍是就用普通方式吧,实在须要在按上面的来处理吧。code

 

2.新建分部视图:右击Index,勾选建立为分部视图:htm

视图内容:blog

<p>@ViewBag.Time</p>

3.在须要加载此分部视图的主视图页面添加该分部视图:get

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Index</h2>

@Html.Action("Index", "PartialCache")

 

效果:

 

参考来源:

MVC缓存的使用

 

扩展阅读:

MVC3缓存(一):页面缓存

MVC3缓存(二): 页面局部缓存

ASP.NET MVC3缓存之一:使用页面缓存

相关文章
相关标签/搜索