Set Page Output Cache VaryByCustom value programmatically
I w开发者_StackOverflow社区ant 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?
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);
}
精彩评论