In ASP.NET, is it possible to output cache by host name? ie varybyhost or varbyhostheader?
Asked Answered
P

2

7

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)

  1. www.foo.com
  2. 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?

Ptero answered 11/5, 2010 at 1:57 Comment(3)
In addition to the answers; I think varyByHeader="host" should suffice, without the need for a custom vary-by.Czardas
Possible duplicate of ASP.NET MVC output cache for multinenant application, vary by hostname and cultureNewel
If you are using asp.net core mvc try this answerJackie
S
13

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();
}
Solarium answered 31/1, 2011 at 12:32 Comment(0)
S
5

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

Squalene answered 11/5, 2010 at 8:0 Comment(3)
Dude -that really does help. If anything, I can now spend some time researching this. I'll return in a week or so with some answers when I next get some time. But seriously - thanks for the idea .. it's very very very helpful!!Ptero
Glad that this has been helpful. Just thinking, you might want to use HttpContext.Current.Request.Url.Host to determine the host name that the user is using to connect to your site. You can then search that string for the substring '.com' or '.com.au' and just return it - ASP.NET will do the rest.Squalene
Cheers ... i've got the hostname thing sorted out (cause our site is themed via hostname). I just need some time to R&D this varybycustom... too much work ...Ptero

© 2022 - 2024 — McMap. All rights reserved.