I've got a website that has a number of host headers. The theme and data depend on the host header, and different hosts load different looking sites.
So let's imagine I have a website called "Foo" that returns search results. The same code runs both sites listed below. It is the same server and website (using Host Headers)
www.foo.com
www.foo.com.au
Now, if I go to .com
, the site is themed in blue. If I go to the .com.au
site, it's themed in red.
And the data is different for the same search result, based on the host name: US results for .com
and Australian results for .com.au
.
If I wish to use OutputCaching
, can this be handled and partitioned by the host name?
I'm concern that after a person goes to the .com
site, (correctly returning US results) that a second person visiting the .com.au
site and searching for the same data will get the theme and results for the .com
site.
Is caching possible?
Yes, you can "vary by custom". I am using the same:
Place the following in your Global.asax.cs:
public override string GetVaryByCustomString(HttpContext context, string custom)
{
if (custom == "Host")
{
return context.Request.Url.Host;
}
return String.Empty;
}
Then in your controller:
[OutputCache(VaryByParam = "None", VaryByCustom="Host", Duration = 14400)]
public ActionResult Index()
{
return View();
}
Check out the VaryByCustom parameter of the OutputCache directive.
To define what happens when VaryByCustom is called, you need to override the method GetVaryByCustomString:
public override string GetVaryByCustomString(HttpContext context, string custom)
{
if(custom == "Your_Custom_Value")
{
// Do some validation.
// Return a string for say, .com, or .com.au
}
return String.Empty;
}
The key is to return a string value for each instance you want to cache. In your case, your overrided method would need to strip the ".com" or ".com.au" part from the URL and return it. Each different value produces a different cache.
HTH
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With