Kaicui
Kaicui

Reputation: 3863

Server Cache bug with OutputCache Attribute?

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?

Upvotes: 0

Views: 1141

Answers (1)

Evgeniy Labunskiy
Evgeniy Labunskiy

Reputation: 2042

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

Upvotes: 1

Related Questions