I've just figured out how to achieve this.
Simply use the VaryByHeader
property, set to "host"
. There are many possibilities to do so.
Method 1
Use the OutputCacheAttribute
passing all the needed configuration elements, including VaryByHeader
:
public class HomeController : Controller
{
[OutputCache(Duration = 3600, VaryByParam = "none", VaryByHeader = "host")]
public ActionResult Index() { /* ... */ }
}
Method 2.
Or you could set it to a profile on the 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>
Then use it:
public class HomeController : Controller
{
[OutputCache(CacheProfile = "Multitenant")]
public ActionResult Index() { /* ... */ }
}
Method 3.
Or you can subclass the OutputCacheAttribute
and use it:
public sealed class MultitenantOutputCacheAttribute : OutputCacheAttribute
{
public MultitenantOutputCacheAttribute()
{
VaryByHeader = "host";
VaryByParam = "none";
Duration = 3600;
}
}
Then use it:
public class HomeController : Controller
{
[MultitenantOutputCache]
public ActionResult Index() { /* ... */ }
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…