automaton
automaton

Reputation: 3108

OutputCache attribute on a controller

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.

Upvotes: 0

Views: 1601

Answers (1)

MichaC
MichaC

Reputation: 13380

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).

Upvotes: 1

Related Questions