I am attempting to write a common "menu.ascx" usercontrol in Asp.Net MVC that will generate a properly formatted HTML menu for my application. The menu is generated based on content in the database and a series of Resource resolutions... which are passed to the PartialView through an attribute on a ViewModel.
It would make sense to utilize an OutputCache directive on the menu.ascx control in order to limit the number of round-trips to the database and Resource files. My intention is to mark the OutputCache directive with VaryByParam=none and VaryByCustom attributes, implementing a custom security lookup in global.asax...
My question is: how do we know when the OutputCache for menu.ascx is going to be used, so that we can skip the data fetch operations when constructing the ViewModel in the controller?
Some sample UserControl code:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<%@ OutputCache VaryByParam="none" VaryByCustom="customstring" %>
<ul>
<% var model = (IMyViewModel)Model;
foreach (var menu in model.Menus) { %>
<li><a href="<%= menu.href %>"><%= menu.Text %></a></li>
<% } %>
</ul>