Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Page Output Cache VaryByCustom value programmatically

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?

like image 301
Chris Marisic Avatar asked Apr 15 '10 18:04

Chris Marisic


People also ask

What is page output caching and post-cache?

Page Output caching enables caching of individual web pages. Post-Cache Substitution exempts fragments of a web page from caching. With cache profiles, cache settings can be specified for a group of web pages. The <ouputCache> and <outputCacheSettings> elements in Web.Config files are used to configure cache settings in Page Output Caching.

What is output caching in MVC?

In this article you will learn everything about Output Caching in MVC. Output Caching enables us to cache the content returned by any controller method so that the same content does not need to be generated each time the same controller method is invoked.

How does the output cache work?

It is very important to understand how the "Output Cache" works. Anyone who invokes a controller method will get the same cached version of the view page. This means that the amount of work that the web server must perform to serve the view page is dramatically reduced.

How do I cache a web page in ASP NET?

ASP.NET provides caching of web pages through Page Output Caching. A page is enabled for caching using the @OutputCache directive. This directive is written at the top of the page which is to be cached. The code below shows the code in the hold to cache a web page for 60 seconds.


1 Answers

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); 
}
like image 59
David Ebbo Avatar answered Oct 12 '22 20:10

David Ebbo