Set Page Output Cache VaryByCustom value programmatically
Asked Answered
P

1

7

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?

Prismatic answered 15/4, 2010 at 18:22 Comment(0)
B
8

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); 
}
Butt answered 27/4, 2010 at 20:2 Comment(2)
This looks like the answer I was looking for however looking at 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?Prismatic
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: #1043612Butt

© 2022 - 2024 — McMap. All rights reserved.