0

I have a ASP.NET MVC 4 controller that looks like this:

#if !DEBUG
    [OutputCache]
#endif
    public class LearningController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Articles(string name)
        {
            ... return dynamic content here based on name
        }
    }

Then I have a RouteConfig which maps "name" to the URL, like this:

        routes.MapRoute(
            name: "Articles",
            url: "learning/articles/{name}",
            defaults: new { controller = "Learning", action = "Articles" }
        );

The caching appears to work. When I set a @DateTime.Now in the .cshtml file and use release, it is indeed caching. Additionally, each article (by name) is returning the dynamic content correctly as well. What's more, is if I revert to query string (remove the MapRoute altogether), it still all works correctly.

Can anyone explain to me why this is working properly without a VaryByParam? I ask because I'm concerned that the dynamic action is not caching properly, or when I go into production might start serving incorrect content.

automaton
  • 1,972
  • 5
  • 25
  • 40

1 Answers1

1

The {name} parameter is part of the URI because you added it to your route, and OutputCache always caches each URI independently. VaryByParam would only affect the query string of HttpGet methods, e.g. /learning/articles?name=abc would cache /learning/articles/ unless you defined VaryByParam="name" (or * instead of name).

MichaC
  • 13,104
  • 2
  • 44
  • 56
  • I agree with the URI part, but as mentioned if you remove the routing (so it is using querystring) it still works properly. Accessing ?name=abc and ?name=xyc still yields different pages, despite the `OutputCache`. This is the part I do not understand. – automaton Feb 18 '16 at 16:17
  • MVC works with that route per default, doesn't matter if you call it {name} or {id}, if you url is /controller/action, it will always work like that – MichaC Feb 18 '16 at 16:27