简体   繁体   English

ASP.NET Core ResponseCacheAttribute-VaryByCustom?

[英]ASP.NET Core ResponseCacheAttribute - VaryByCustom?

I've been using Microsoft.AspNetCore.Mvc.ResponseCacheAttribute for the first time and have come across an issue that I would have solved previously using the VaryByCustom property in OutputCache in ASP.NET (and using public override string GetVaryByCustomString(HttpContext context, string s) in the global.asax). 我第一次使用Microsoft.AspNetCore.Mvc.ResponseCacheAttribute ,并且遇到了一个我以前使用ASP.NET的OutputCache中的VaryByCustom属性可以解决的问题(并使用public override string GetVaryByCustomString(HttpContext context, string s)在global.asax中)。

This VaryByCustom caching seems to no longer exist in ASP.NET Core. 这种VaryByCustom缓存似乎在ASP.NET Core中不再存在。 Is there a built-in alternative that I'm missing here or will I need to implement this myself to achieve something similar? 我是否在这里缺少内置的替代方法,或者我需要自己实现此功能才能实现类似的目的?

From my understanding, you have two flexible options in ASP.NET core: 据我了解,ASP.NET核心中有两个灵活的选项:

  1. Use the VaryByHeader or VaryByQueryKeys if you're using the ResponseCacheAttribute . 如果您使用的是ResponseCacheAttribute请使用VaryByHeaderVaryByQueryKeys

When using Headers, you need to write the value to vary by as a header, which could be any arbitrary value (no need to expose data to the client): 使用标头时,您需要编写一个值以作为标头而有所不同,标头可以是任意值(无需向客户端公开数据):

Response.Headers.Add("X-My-Vary-Header", "this-is-variable");

In essence, this is all the VaryByCustomString ever did for you anyway. 本质上,这就是VaryByCustomString曾经为您所做的所有VaryByCustomString The way I see it, you're no longer forced to put this code in a specific method/file (eg global.asax). 从我的角度来看,您不再被迫将此代码放入特定的方法/文件(例如global.asax)中。

  1. Try the <cache> Tag Helper when caching in Razor. 在Razor中进行缓存时,请尝试<cache>标记助手。

Here you have a wide range of things to "vary" by: vary-by-header , vary-by-route , vary-by-user , and even a custom vary-by . 在这里,您可以通过“ vary-by-header ,“ vary-by-route ,“ vary-by-user甚至“自定义”来vary-by

Have a look here and decide whether to use the attribute or the cache tag helper: https://docs.microsoft.com/en-us/aspnet/core/mvc/views/tag-helpers/built-in/cache-tag-helper 在这里看看并决定是使用属性还是使用缓存标记帮助器: https : //docs.microsoft.com/zh-cn/aspnet/core/mvc/views/tag-helpers/built-in/cache-tag -帮手

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM