views:

23

answers:

1

The following code is not working in a partial view.

<%
  // Pages always expire at midnight.
  Response.Cache.SetExpires(DateTime.Today.AddHours(24));

  if (variable > 0)
  {
      Response.Cache.SetCacheability(HttpCacheability.ServerAndPrivate);
  }
  else
  {
      Response.Cache.SetCacheability(HttpCacheability.Public);
  }
%>

Can anyone tell me why? The partial view is never cached and there is no apparent reason. Should I write code to activate the cache? How?

A: 

Try putting the code into the control's codebehind file Page_Load or Page_Init event. I suspect that because your code is inline in your ascx file, it is being executed too late in the lifecycle of the control.

Daniel Dyson
It's ascx file, I tried putting the code in the Page_Load, but with no results.If I select the address bar and press ENTER, it uses the cached version, but I click a link that takes to the same page or refresh the page, then the server renders the page again.
Fabio Milheiro
This partial view is not associated to any action. Should I set the partial view to inherit from an action instead of an object from the view and set the outputcache there? (In the action...)
Fabio Milheiro