asp.net mvc4 VaryByParam does not work

137 views Asked by At

My code is as below:

[HttpGet]
[OutputCache(Duration = 90, VaryByParam = "cityCode")]
public ActionResult About(string userName, string cityCode)
{
     //do something...
     return View();
}
  1. the cache works fine when I access the URL:

http://localhost:52121/LabOne/MvcCache/About?userName=admin&cityCode=010

  1. but when I access this route URL as below, the cache does not work, why?

http://localhost:52121/LabOne/MvcCache/About/admin/010

1

There are 1 answers

0
Hakan Fıstık On BEST ANSWER

I copied your code and I tested it on my machine and I configured the RouteConfig as the following

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

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

        routes.MapRoute(
           name: "aboutRoute",
           url: "{controller}/{action}/{userName}/{cityCode}",
           defaults: new { controller = "Home", action = "About", userName = UrlParameter.Optional, cityCode = UrlParameter.Optional  }
       );
    }
}

And I faced the same problem, I will explain this :

The OutputCache is depending on the URL, and the examples you provides is really two different URLs, although they are will make the same result.

So try to request the URL http://localhost:52121/LabOne/MvcCache/About/admin/010 one more time. and you will see that OutputCache is working, and the MVC will bring the result from cache, because the OutputCache has been cached this URL on previous time.

UPDATE

according to this question Using outputcache in MVC and its accepted answer, the Caching are working with the URL and has no relevance with the MVC Routing System.