簡體   English   中英

控制器上的OutputCache屬性

[英]OutputCache attribute on a controller

我有一個ASP.NET MVC 4控制器,看起來像這樣:

#if !DEBUG
    [OutputCache]
#endif
    public class LearningController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Articles(string name)
        {
            ... return dynamic content here based on name
        }
    }

然后,我有一個RouteConfig,它將“名稱”映射到URL,如下所示:

        routes.MapRoute(
            name: "Articles",
            url: "learning/articles/{name}",
            defaults: new { controller = "Learning", action = "Articles" }
        );

緩存似乎可以正常工作。 當我在.cshtml文件中設置@DateTime.Now並使用release時,它確實是緩存。 此外,每篇文章(按名稱)也正確地返回了動態內容。 更重要的是,如果我恢復為查詢字符串(完全刪除MapRoute),則仍然可以正常工作。

誰能向我解釋為什么沒有VaryByParam可以正常工作? 我問是因為我擔心動態操作不能正確緩存,否則在我投入生產時可能會開始提供不正確的內容。

{name}參數是URI的一部分,因為您已將其添加到路由中,並且OutputCache始終始終獨立緩存每個URI。 VaryByParam僅會影響HttpGet方法的查詢字符串,例如/learning/articles?name=abc將緩存/learning/articles/除非您定義了VaryByParam="name" (或*而不是name )。

暫無
暫無

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

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