我刚刚想出了如何实现这一目标。
只需使用VaryByHeader
属性,设置为"host"
. 这样做有很多可能性。
方法一
使用OutputCacheAttribute
传递所有需要的配置元素,包括VaryByHeader
:
public class HomeController : Controller
{
[OutputCache(Duration = 3600, VaryByParam = "none", VaryByHeader = "host")]
public ActionResult Index() { /* ... */ }
}
方法2。
或者您可以将其设置为 Web.config 上的配置文件:
<?xml version="1.0"?>
<configuration>
<!-- ... -->
<system.web>
<!-- ... -->
<caching>
<outputCacheSettings>
<outputCacheProfiles>
<clear/>
<add name="Multitenant"
enabled="true"
duration="3600"
varyByHeader="host"
varyByParam="none"/>
</outputCacheProfiles>
</outputCacheSettings>
</caching>
</system.web>
</configuration>
然后使用它:
public class HomeController : Controller
{
[OutputCache(CacheProfile = "Multitenant")]
public ActionResult Index() { /* ... */ }
}
方法3。
或者你可以继承OutputCacheAttribute
并使用它:
public sealed class MultitenantOutputCacheAttribute : OutputCacheAttribute
{
public MultitenantOutputCacheAttribute()
{
VaryByHeader = "host";
VaryByParam = "none";
Duration = 3600;
}
}
然后使用它:
public class HomeController : Controller
{
[MultitenantOutputCache]
public ActionResult Index() { /* ... */ }
}