views:

338

answers:

1

I want to use an Enum value for the types of VaryByCustom parameters I will support, is it possible to do this?

I tried setting it in the page itself

<%@ OutputCache Duration="600" VaryByParam="none" 
            VaryByCustom='<%=VaryByCustomType.IsAuthenticated.ToString(); %>' %>

But this returned the entire literal string "<%=VaryByCustomType.IsAuthenticated.ToString(); %>" inside my global.asax is there any way to do this either on the page itself or from the codebehind? Or is this just something I have to accept is purely magic strings and nothing I can do to add type safety to it?

+3  A: 

Instead of using the @Outputcache directive, try doing it with code in the page. e.g.

void Page_Init() {
    var outputCacheSettings = new OutputCacheParameters() {
        Duration = 600,
        VaryByCustom = VaryByCustomType.IsAuthenticated.ToString()
    };
    InitOutputCache(outputCacheSettings); 
}
David Ebbo
This looks like the answer I was looking for however looking at http://msdn.microsoft.com/en-us/library/ms153473.aspx it has under remarks not to use this method, any chance to do you have any knowledge on why the MSDN would say that yet still expose this method? Or what implications it has to invoke it myself?
Chris Marisic
Frankly, this is outdated documentation that dates back from the 2.0 days and was never updated. This is actually the call that gets generated for you when you use the directive.As an alternative, you can also use the technique described in this thread: http://stackoverflow.com/questions/1043112/programatically-control-output-caching-disable-or-enable-cache-according-to-par
David Ebbo
Thanks alot David!
Chris Marisic