Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Turn off page-level caching in a user control

I have a page with the following caching defined:

<%@ OutputCache Duration="60" VaryByParam="None" %>

I have a user control inside that page that i don't want cached. How can I turn it off just for that control?

like image 625
Micah Avatar asked Jul 23 '10 13:07

Micah


People also ask

How should you enable Output caching?

Enabling Output Caching. You enable output caching by adding an [OutputCache] attribute to either an individual controller action or an entire controller class. For example, the controller in Listing 1 exposes an action named Index(). The output of the Index() action is cached for 10 seconds.

What is Output caching in mvc?

The output cache enables you to cache the content returned by a controller action. Output caching basically allows you to store the output of a particular controller in the memory. Hence, any future request coming for the same action in that controller will be returned from the cached result.

Where is Output cache stored?

The output cache is located on the Web server where the request was processed. This value corresponds to the Server enumeration value. The output cache can be stored only at the origin server or at the requesting client. Proxy servers are not allowed to cache the response.


1 Answers

Option One

Use the Substitution control or API on your page. this enables you to cache everything on your page except the part contained within the substitution control.

http://msdn.microsoft.com/en-us/library/ms227429.aspx

One nice way to use this is to implement your control as a simple server control which renders the html as a string, but does so in the context of the page (that is with the correct Client IDs). Scott Guthrie has a really nice example of how this works. Works nicely with AJAX calls too by the way...

http://weblogs.asp.net/scottgu/archive/2006/10/22/Tip_2F00_Trick_3A00_-Cool-UI-Templating-Technique-to-use-with-ASP.NET-AJAX-for-non_2D00_UpdatePanel-scenarios.aspx

Excerpt from Scott Gu's article...

    [WebMethod]
    public string GetCustomersByCountry(string country)
    {
       CustomerCollection customers = DataContext.GetCustomersByCountry(country);

        if (customers.Count > 0)
            //RenderView returns the rendered HTML in the context of the callback
            return ViewManager.RenderView("customers.ascx", customers);
        else
            return ViewManager.RenderView("nocustomersfound.ascx");
    }

Option Two

Render the dynamic control via an AJAX call on the page load. This way, you can safely cache the entire page (including the AJAX call) and it is only the rendered result of the call that changes between pages.

like image 137
Daniel Dyson Avatar answered Oct 27 '22 18:10

Daniel Dyson