Search code examples
c#asp.net-coreasp.net-core-tag-helpers

Why does ASP.NET Core CacheTagHelper vary-by-route not refresh Cache on route change?


I've got the following controller method defined that creates two different asp.net core routes (named route1 and route2). I'm expecting that when I use the cshtml below that when I call:

localhost/route1
localhost/route2

that my cache will be replaced but it is not.

(Having the parameter {param} passed in does not change my expected results)

Controller Page

    [Route("/route1/{param}", Name = "route1")]
    public IActionResult Route1(string param)
    {
        ViewData["Message"] = "route1.";
        return View("CacheTagHelper/vary-by-route");
    }

    [Route("/route2/{param}", Name = "route2")]
    public IActionResult Route2(string param)
    {
        ViewData["Message"] = "route2.";
        return View("CacheTagHelper/vary-by-route");
    }

View Page

<Cache vary-by-route="route1,route2">Time Inside Cache Tag Helper : @DateTime.Now</Cache>

Solution

  • There is no route value keyed route1/route2. Aka your cache TagHelper should be:

    <cache vary-by-route="param">Time Inside Cache Tag Helper : @DateTime.Now</cache>