views:

297

answers:

4

How could I cache an entire page except a bit at the top which says something along the lines of "Welcome back, Matt! | Log Out" if the user is logged in and so-on?

I'm using ASP.NET MVC 2.

+2  A: 

Probably helps

<%@ OutputCache Duration="15" VaryByParam="*" %>

or with some other value for the VaryByParam. See http://msdn.microsoft.com/en-us/library/hdxfb6cy.aspx, http://blog.maartenballiauw.be/post/2008/06/Creating-an-ASPNET-MVC-OutputCache-ActionFilterAttribute.aspx and http://blogs.microsoft.co.il/blogs/gilf/archive/2010/07/18/asp-net-output-cache-provider.aspx.

Moreover, if you have the start page which is not user depended, it is possible to replace the start page with a very static welcome page with the empty field (hidden div) instead of "Welcome back, Matt! | Log Out". After that an ajax request for filling of the user name can be started at the client side. This kind of the welcome page page can be very good cached.

Oleg
In Matt's case this means that a separate version of the page will be put in cache for every value of the parameter(every user-name in his case).
Branislav Abadjimarinov
Good point, although retrieving the username via ajax is an interesting idea. I could also try retrieving the username from a cookie, to prevent making an extra request to the server.
Matt H
I thought one must avoid the `OutputCache` page directive in MVC application and use action filters instead. :S
Robert Koritnik
+4  A: 

What you are trying to achieve is called donut-caching or cache substitution. As of ASP.NET MVC 2 there is no built in helper to support this scenario. As much as I know it was a planned feature in MVC v.1 but it was dropped somewhere in the way to the release. For more info check this links http://haacked.com/archive/2008/11/05/donut-caching-in-asp.net-mvc.aspx, http://stackoverflow.com/questions/211827/is-donut-caching-working-properly-with-asp-net-mvc. VaryByParam option that is mentioned by Oleg here is not a good idea in your case. If you have VaryByParam a different version of the page will be put in the cache for every different value of the parameter (in your case for every user-name). Personally I would think of caching the data, not the whole output of the page.

Branislav Abadjimarinov
A: 

Here you have a workaround solution:

*Add the OuptutCache attribute to the Controller that manages the whole view as usually:

[OutputCache(Duration = 3600, VaryByParam = "*")]
public ActionResult Index(FormCollection formCollection)
{
   //Controller code
}

*For the part that you don't want to do caching, load it using jquery + an ajax request (with its own controller and without the OutputCache attribute):

<div id="loginContainer"></div>

$(document).ready(function() {

    $.post('controller/action', postdata, function(data) {

    if (data.success == true) {

        //Populate the container with the obtained data
    }
    });

});

The view will be retrieved from the Output Cache and, once it is loaded, a request to obtain the login info will be performed. Hopefully, it will be a very quick request and the user will not notice the delay.

despart
And what happens when JavaScript is disabled on the client?
rockinthesixstring
@rock it won't work.
Rangoric