簡體   English   中英

MVC如何使用OutputCache屬性緩存要在多個頁面上使用的操作的結果

[英]MVC How to cache the results of an action to be used on multiple pages with OutputCache attribute

我們的應用程序中有一個實例,我們希望能夠使用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動作,

[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("");
}

您可以嘗試將請求定向到Web api控制器,然后在其中構建簡單的手動緩存機制。 可以在此處找到一個示例: Web API中的緩存

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM