in my asp.net mvc project, I enable output caching on a controller as below
[OutputCache(Duration = 100, VaryByParam = "*", VaryByHeader = "X-Requested-With")]
public class CatalogController : BaseController
{
public ActionResult Index(string seller)
{
// I do something
}
}
it works great, until create my own Route class as below
public class MyRoute : Route
{
// there is a constructor here..
// I override this method..
// just to add one data called 'seller' to RouteData
public override RouteData GetRouteData(HttpContextBase httpContext)
{
var data = base.GetRouteData(httpContext);
if (data == null) return null;
var seller = DoSomeMagicHere();
// add seller
data.Values.Add("seller", seller);
return data;
}
}
and then, the action method will take seller
as parameter. I tested it by always providing different seller
parameter, but it take the output from cache instead of calling the method.
does setting VaryByParam="*" also vary by RouteData.Values, in asp.net mvc?
I'm using ASP.Net 4 MVC 3 RC 2
The output caching mechanism varies by URL, QueryString, and Form. RouteData.Values is not represented here. The reason for this is that the output caching module runs before Routing, so when the second request comes in and the output caching module is looking for a matching cache entry, it doesn't even have a RouteData object to inspect.
Normally this isn't a problem, as RouteData.Values comes straight from the URL, which is already accounted for. If you want to vary by some custom value, use VaryByCustom and GetVaryByCustomString to accomplish this.
If you remove VaryByParam = "*" it should use your action method parameter values when caching.
ASP.NET MVC 3’s output caching system no longer requires you to
specify a VaryByParam property when declaring an [OutputCache]
attribute on a Controller action method. MVC3 now automatically
varies the output cached entries when you have explicit parameters on your action method – allowing you to cleanly enable output...
Source: http://weblogs.asp.net/scottgu/archive/2010/12/10/announcing-asp-net-mvc-3-release-candidate-2.aspx
[OutputCache(Duration = 100, VaryByHeader = "X-Requested-With")]
public class CatalogController : BaseController
{
public ActionResult Index(string seller)
{
// I do something
}
}