开发者

.NET MVC 3 OutputCache Html.Action()

I am using OutputCache(Duration = 60) on an action and by default I thought child actions rendered with @Html.Action("ActionName", "Controll开发者_高级运维erName") would not be cached unless annotated with OutputCache? This worked with MVC 2 but does not seem to be working with MVC 3. If this has changed, how would I set a portion of the page NOT to cache?

Thanks


I'm not sure, but this looks similar, perhaps it will help:

OutputCache and RenderAction Cache Whole Page


This problem is very frustration. It seems like duration should be able to be set to 0 for child actions as you can for normal actions, but because it has to be a positive integer greater than 0 you get a "duration must be a positive number" error if you try to set duration to 0 for child actions. To solve this I decorate child actions with these attributes:

[ChildActionOnly]
[OutputCache( Duration = 1, VaryByCustom="Always")]

Then I setup the Always custom output cache in the Global.asax.cs by adding the following method:

    public override string GetVaryByCustomString(HttpContext context, string arg)
    {
            if (arg == "Always")
            {
                    return DateTime.Now.Ticks.ToString();
            }

            return String.Empty;
    }

Basically the first attribute is telling MVC that this is a child action (not reachable from outside the app). The second attribute is saving the cache for 1 second (minimum allowed for child actions) and varying the cache based on my custom Always caching. In the method in the global.asax.cs, I am trying to make the value returned unique enough that it will never find it in the one second of caching that is happening. Not bulletproof, but should work for website that don't have a ton of traffic.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜