asp.net - 在 Controller 中缓存 ASP.NET 5

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

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

我在 ASP.NET MVC 5 Controller 中这样做:

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

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

Controller 属性:

[ResponseCache(CacheProfileName = "TestCache")]

在我的Startup.cs中:

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

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

我该如何解决?

最佳答案

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

[ResponseCache(Duration=60)]

对应于

[OutputCache(Duration = 60)]

它放置 HTTP header

Cache-Control: public,max-age=60

在相应的 HTTP 响应中。

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

关于asp.net - 在 Controller 中缓存 ASP.NET 5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35129324/

相关文章:

javascript - anchor 标记事件未触发

asp.net-mvc - 将构造函数参数传递给 MVC Controller 中的注入(inject)对象

c# - 如何防止 ASP.net MVC 应用程序通过域身份验证?

jquery - JsTree使用带有参数的ajax加载所有数据

c# - 如何在asp.net core razor pages中实现分页

c# - 需要帮助检查用户并将其添加到多个异步角色

javascript - 在asp.net中滚动文本

c# - .Net Core API Controller 接受表单或正文数据

c# - 如何检查 QueryString 是否没有参数

c# - DateTime "must be a date"仅带斜杠