I have the following code in my MVC controller:
[HttpGet]
[OutputCache(Duration = 3600, VaryByParam = "none", VaryByCustom = "app")]
public async Task<ViewResult> Index(string r)
{
// Stuff...
}
And I have the following implementation of GetVaryByCustomString in my Global.asax.cs class:
public override string GetVaryByCustomString(HttpContext context, string arg)
{
switch (arg.ToLower())
{
case "app":
return context.Request.Url.Host;
default:
return base.GetVaryByCustomString(context, arg);
}
}
In our application, customers will have their own subdomain (i.e. johndoe.app.com, janedoe.app.com).
So caching should vary on subdomain.
However, any "path" on that fully qualified URL should share the same cache. So the following should read the same output cache:
- johndoe.app.com/
- johndoe.app.com/123
- johndoe.app.com/abc
There's an exhausting reason why this is the way it is, but in short, it's a SPA app, and the "path" is really just a tracker. This can't be changed to a query string.
When the path (tracker) changes, the index method is freshly accessed. I can tell this through the debugger. As a note, GetVaryByCustomString
is still called, but it's called after the Index method has been processed.
How can I vary cache based on subdomain, but use that cache regardless of the path (tracker) on the URL?
If it offers anything benefical, here's my MVC routes:
routes.MapRoute(
name: "Tracker",
url: "{r}",
defaults: new { controller = "Home", action = "Index", id = "" });
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
MVC version 5.2.3, .NET 4.6.1