views:

44

answers:

2

Hi,

I have a partial view that should not be cached in a output cached MVC view. Usually you write non-cached content by using Response.WriteSubstitution. The problem is that WriteSubstitution takes as a parameter a HttpResponseSubstitutionCallback callback which looks like this:

public delegate string HttpResponseSubstitutionCallback(System.Web.HttpContext context)

This is where things get complicated since there is no easy/fun way to generate the html on the fly. You have to do a hack like this.

So the question is: Is there an easier way to make a partial view not cached ?

A: 

See Phil Haack's article on donut caching in MVC. Phil takes advantage of the existing API to create a new HtmlHelper method to provide a callback that can render the non-cached code. His supplies an anonymous method to the helper to specify the callback. To get this to work unchanged, you'll still need to have a method that renders a partial view to a string, though I think it would be easier to do in an HtmlHelper -- just look at the source for RenderPartial and RenderPartialInternal and see what changes would be needed to write it to a MemoryStream instead of the Response -- I believe it would be the same code except you'd supply your stream instead of the Response output stream, then convert your stream to a string.

It might look like this:

<%= Html.Substitute( cb => Html.RenderToString( "Partial" ) ) %>

Phil indicates that it might make it in to ASP.NET MVC 1.0, but I think it's only available in the MvcFutures assembly.

tvanfosson
A: 

In MVC2 we can use Html.Action to easily obtain the substituted html. Yeeeeiii
But Response.WriteSubstitotion is not working anymore. Aaaahhhh