【问题标题】:MVC How to cache the results of an action to be used on multiple pages with OutputCache attributeMVC 如何使用 OutputCache 属性缓存要在多个页面上使用的操作结果
【发布时间】:2017-03-13 17:50:52
【问题描述】:

我们的应用程序中有一个实例,我们希望能够使用 OutputCache 为多个页面缓存 MVC 操作的结果,但我刚刚意识到我为每个页面单独设置了基本设置缓存,因此这会中断链条。

我的最终目标是,当所有其他页面完成加载并缓存后续页面的数据后,用户点击主页执行 ajax 请求。然后,当用户导航到需要 MVC 操作数据的另一个页面时,它立即可用。

这是我在主页上异步加载数据的脚本,

$(document).one("ajaxStop", function() {

    $.ajax({
        url: '../Companies/CompanySelectList',
        type: 'GET',
        success: function (data) {
            //alert(data.success);
        },
        error: function (request, status, error) {
            //alert(request.responseText);
        }
    })

})

这是我要执行的 MVC Action,

[OutputCache(Duration=60)]
public ActionResult CompanySelectList()
{
    List<CompanyDTO> companies = new List<CompanyDTO>();

    var results = _api.Companies.GetCompany();
    if (results.message == null)
    {
        companies.AddRange(results.data);

        //Build up remaining data
        for (int i = results.page + 1; i <= results.totalPages; i++)
        {
            results = _api.Companies.GetCompany(page: i);
            companies.AddRange(results.data);
        }

        companies = companies.OrderBy(n => n.CompanyName).ToList();
        return PartialView("_CompanySelectList", companies);
    }
    else
    {
        ModelState.AddModelError("", results.message);
    }

    return PartialView("");
}

【问题讨论】:

    标签: c# jquery ajax asp.net-mvc caching


    【解决方案1】:

    您可以尝试将请求定向到 Web api 控制器,并在其中构建一个简单的手动缓存机制。 一个例子可以在这里找到:Caching in Web API

    【讨论】:

      猜你喜欢
      • 2012-05-01
      • 2015-12-03
      • 1970-01-01
      • 2012-04-18
      • 2010-10-11
      • 2012-05-26
      • 1970-01-01
      • 2013-09-01
      相关资源
      最近更新 更多