如何将旧的 .NET OutputCaching 转换为 .NET 8.0 OutputCaching?

问题描述 投票:0回答:1

下面的代码非常适合我的 .NET 4.5 网站:

[OutputCache(VaryByParam = "*", Duration = 60, NoStore = true, Location = OutputCacheLocation.Server)] // 1 MINUTE CACHE
public JsonResult MyController(...)
{ ... }

当尝试将其用于我的全新 .NET 8.0 项目时,我立即收到

VaryByParam
Location
的错误。也许是两个最重要的设置。我特别希望缓存位于服务器端,因为我有许多设备请求数据,如果已经有一个设备,我只希望其他设备获得相同的答复,而不在链中进一步生成请求。

我尝试过这样的调整

[OutputCache(Duration = 60, NoStore = true, VaryByQueryKeys = ["*"])]

但是它不起作用,而且它肯定不会告诉服务器将其缓存在我需要它做的服务器端。

如何将旧类型 .NET OutputCaching 转换为具有相同属性的新 .NET 8.0 OutputCaching,尤其是服务器端缓存?

c# .net caching server-side .net-8.0
1个回答
0
投票

OutputCacheAttribute
NoStore
定义为:

/// <summary>
/// Gets or sets the value which determines whether the response should be cached or not.
/// When set to <see langword="true"/>, the response won't be cached.
/// </summary>
public bool NoStore
{
    get => _noCache ?? false;
    init => _noCache = value;
}

因此,如果将其设置为

true
,它实际上会禁用输出缓存。

另外,不要忘记在适当的地方致电

UseOutputCache()
- 请参阅示例@github

var app = builder.Build();

app.UseOutputCache();
© www.soinside.com 2019 - 2024. All rights reserved.