Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
596 views
in Technique[技术] by (71.8m points)

asp.net mvc - Does VaryByParam="*" also read RouteData.Values?

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

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...