在Controller中的ASP.NET 5中缓存

时间:2016-02-01 11:24:42

标签: asp.net asp.net-mvc asp.net-core asp.net-core-mvc asp.net-caching

我正在尝试像在ASP.NET MVC 5中那样缓存输出控制器。

我在ASP.NET MVC 5 Controller中做到了这一点:

 [OutputCache(Duration = 60, VaryByParam = "*", Location = OutputCacheLocation.ServerAndClient)]

现在,我正在ASP.NET 5 MVC 6中尝试:

控制器属性:

[ResponseCache(CacheProfileName = "TestCache")]

在我的 Startup.cs

//Caching
            services.Configure<MvcOptions>(options => options.CacheProfiles.Add("TestCache", new CacheProfile()
            {
                Duration = 3600,
                Location = ResponseCacheLocation.Any,
                VaryByHeader = "*"
            }));

我在TestController中添加了断点,但每次都会触发breakboint。

我该如何解决?

1 个答案:

答案 0 :(得分:0)

您应该使用描述为here的MVC操作的新属性。例如

[ResponseCache(Duration=60)]

对应

[OutputCache(Duration = 60)]

它放置HTTP标头

Cache-Control: public,max-age=60

在相应的HTTP响应中。

如果您更喜欢使用缓存配置文件,则可以在同一篇文章中找到有关使用情况的相应信息(请参阅here)。