title: How to implement VaryByCustom caching?
body: <p>I'm trying to implement functionality to cache certain pages depending on the host. This is because I can have multiple versions of a page which have the same parameters, and where the only difference in terms of a request is the host that is being requested.</p>
  
  <p>So, for example these two URLs will request the same page, but they are styled differently:</p>
  
  <pre><code>http://www.a.com/something/specific
  </code></pre>
  
  <p>and </p>
  
  <pre><code>http://www.b.com/something/specific
  </code></pre>
  
  <p>I'm going through the example outlined here:</p>
  
  <p><a href="http://msdn.microsoft.com/en-us/library/5ecf4420%28v=VS.90%29.aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/5ecf4420%28v=VS.90%29.aspx</a></p>
  
  <p>but it's not making sense to me. </p>
  
  <p>I've added this to my global.asax:</p>
  
  <pre><code>public override string GetVaryByCustomString(HttpContext context, string arg)
  {
      if (arg == "host")
      {
          return "host=" + context.Request.Url.Host;
      }
  
      return base.GetVaryByCustomString(context, arg);
  }
  </code></pre>
  
  <p>and the example states "To set the custom string programmatically, call the SetVaryByCustom method and pass it the custom string to use", with code similar to the following:</p>
  
  <pre><code>Response.Cache.SetVaryByCustom("host");
  </code></pre>
  
  <p>The problem is I'm not sure what to do with this. I've added the previous line to <code>MvcApplication_EndRequest</code> because it seems like it makes sense, but I don't think this is right because when I set breakpoints in <code>GetVaryByCustomString</code> they never get hit.</p>
  
  <p>Can somebody please tell me what I'm missing here? Or if I need to do this differently?</p>
  
  <p><strong>Edit:</strong> RE Darin's answer below, I'm already decorating my actions with:</p>
  
  <pre><code>[CustomOutputCache(CacheProfile = "FundScreener")] // or similar depending on the action
  </code></pre>
  
  <p>where <code>CustomOutputCacheAttribute</code> is defined as:</p>
  
  <pre><code>[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
  public class CustomOutputCacheAttribute: OutputCacheAttribute
  {
      public override void OnResultExecuted(ResultExecutedContext filterContext)
      {
          AddLabelFilesDependency(filterContext);
          base.OnResultExecuted(filterContext);
      }
  
      private static void AddLabelFilesDependency(ControllerContext filterContext)
      {
          IConfigurationManager configurationManager = ObjectFactory.TryGetInstance&lt;IConfigurationManager&gt;();
          if (configurationManager == null 
              || filterContext == null 
              || filterContext.RequestContext == null
              || filterContext.RequestContext.HttpContext == null
              || filterContext.RequestContext.HttpContext.Response == null
              )
          {
              return;
          }
          string[] files = Directory.GetFiles(configurationManager.LabelsDirectoryPath, "*.xml");
          foreach(var file in files)
          {
              filterContext.RequestContext.HttpContext.Response.AddFileDependency(file);
          }
      }
  }
  </code></pre>
  
  <p>where the profile is defined as:</p>
  
  <pre><code>&lt;add name="FundScreener"
       location="Server"
       enabled="true"
       varyByParam="*"
       duration="1200"
       sqlDependency="mmftms:offline.ScreenerData"/&gt;
  </code></pre>
  
  <p>Do I need to change this?</p>