Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
582 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...