我知道 OutputCache 尚未为 ASP.NET Core 做好准备,但我已阅读有关 OutputCache 的内容,您可以在 web.config 中配置它,如下所示:
<configuration>
<location path="showStockPrice.asp">
<system.webserver>
<caching>
<profiles>
<add varybyquerystring="*"location="Any"
duration="00:00:01" policy="CacheForTimePeriod"
extension=".asp">
</profiles>
</caching>
</system.webserver>
</location>
</configuration>
我可以配置我的 web.config 以将 OutputCache Web.Config 用于 MVC 路由吗?
例如:
http://www.example.com/View/Index/123562
其中,varyByParam 参数为 123562。
谢谢。
最佳答案
您可以使用IMemoryCache
类来存储结果。 Microsoft 的示例用法可以在 here 中找到。 .
这是一个简单的例子:
public class HomeController : Controller
{
private readonly IMemoryCache _cache;
public HomeController(IMemoryCache cache)
{
_cache = cache;
}
public IActionResult About(string id)
{
AboutViewModel viewModel;
if (!_cache.TryGetValue(Request.Path, out result))
{
viewModel= new AboutViewModel();
_cache.Set(Request.Path, viewModel, new MemoryCacheEntryOptions()
{
AbsoluteExpiration = DateTime.Now.AddHours(1)
});
}
return View(viewModel);
}
}
关于azure - Azure 中具有 MVC 和 ASP.NET Core 的 Outputcache IIS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36714376/