0

i'm a MVC beginner and today i met a strange problem:I want to use OutputCache to enable cache on one action.code like this:

 [OutputCache(Duration=86400,VaryByParam="none")]
    public ActionResult Index(string id)
    {
        ViewBag.Message = "Welcome to ASP.NET MVC!";
        ViewBag.ID = id;

        return View();
    }

Notice that the "VaryByParam" property is "none",yes i want the server keep only one cache for the action, whatever the param was passed. and the routing code is this:

 public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );

    }

then i open the explore,the result is not what i want,for example i type:"http://localhost:27654/home/index/121212",the page come out and id"121212" was shown.but when i changed to "http://localhost:27654/home/index/12",and i see the page was changed ,id"12" was shown.

but if i refresh the page(para "id" not change),the datetime shown in the page didn't change,imply that asp.net has kept the cache VaryBy the "ID" para,not by my set. what's wrong?

Evgeniy Labunskiy
  • 2,012
  • 3
  • 27
  • 45
Kaicui
  • 3,795
  • 1
  • 15
  • 20

1 Answers1

1

Yes. It's because you create another example of page by parameters predefined in route.

[OutputCache(Duration=86400,VaryByParam="none")]
    public ActionResult Index(int id, string some)
    {
        ViewBag.Message = "Welcome to ASP.NET MVC!";
        ViewBag.ID = id;
        ViewBag.Some = some;
        return View();
    }

Route parameters can not be considered parameters for OutputCache. In my example string some is not the part of route, so if you will try the example the new version of cache will not be created if you cange parameter some

Also read this topic: OutputCache Bug with VaryByParam="None" with MVC RC refresh

Community
  • 1
  • 1
Evgeniy Labunskiy
  • 2,012
  • 3
  • 27
  • 45
  • thanks,does that mean in a "Http Get"scene,i can't use Route parameters,i must use queryString to pass parameters?in your example,i must type"http://localhost:27654/home/index?some=wawa" – Kaicui Oct 19 '11 at 08:16
  • it's mean that if any param defined in routes.MapRoute will change - the new example of cache will be created. /home/index/1 or /2 is route parameters, so it will be 2 cache items. QueryString parameters will not apply any influance to cached page, cause VaryByParam="none". Is it make sence? – Evgeniy Labunskiy Oct 19 '11 at 08:24