In ASP.NET, is it possible to display a cache by host name? those. varibyhost or varbyhostheader?

I have a website with multiple host headers. The subject and data depend on the host header, and different hosts load different search sites.

So, imagine that I have a site called "Foo" that returns search results. The same code launches both sites listed below. This is the same server and website (using host headers)

  • www.foo.com
  • www.foo.com.au

Now, if I go to .com , the site will be blue. If I go to the .com.au website, it will be 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 want to use OutputCaching , can this be processed and partitioned by host name?

I’m worried that after a person goes to the .com website (correctly returning the US results), the second person who .com.au website and finds the same data will get the theme and results for the .com website.

Is caching possible?

+4
source share
2 answers

Yes, you can "vary according to custom." I use the same:

Put the following into your Global.asax.cs file:

 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(); } 
+7
source

Check the VaryByCustom parameter of the OutputCache directive.

To determine what happens when VaryByCustom is called, you need to override the GetVaryByCustomString method:

 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 should return a string value for each instance that you want to cache. In your case, your overridden method will have to remove the ".com" or ".com.au" part from the URL and return it. Each other value creates a different cache.

NTN

+4
source

Source: https://habr.com/ru/post/1309378/


All Articles