从控制台应用程序输出一个站点

时间:2013-02-28 15:21:04

标签: c# asp.net-mvc asp.net-mvc-3 console-application outputcache

我想运行一个控制所有outputcacheing页面上的网站的consol应用程序,以便为用户缓存所有缓存的分页。使用浏览器时,页面正确缓存,但我希望能够运行一个为用户缓存所有页面的函数。

我正在使用new System.Net.WebClient().DownloadString("http://url");,但使用此调用时页面不会缓存。

缓存属性

[ChildActionOutputCache(("CacheAll"), Location = OutputCacheLocation.Server)]

属性函数

public class ChildActionOutputCacheAttribute : OutputCacheAttribute
    {
        public ChildActionOutputCacheAttribute(string cacheProfile)
        {
            var settings = (OutputCacheSettingsSection)WebConfigurationManager.GetSection("system.web/caching/outputCacheSettings");
            var profile = settings.OutputCacheProfiles[cacheProfile];
            Duration = profile.Duration;
            VaryByParam = profile.VaryByParam;
            VaryByCustom = profile.VaryByCustom;
        }
    }

属性

的web.config属性
 <caching>
      <outputCacheSettings>
        <outputCacheProfiles>
          <add name="CacheAll" 
               duration="43200"
               location="Server"
               varyByParam="*"/>
        </outputCacheProfiles>
      </outputCacheSettings>
    </caching>

1 个答案:

答案 0 :(得分:0)

我知道这是一个老问题,但是对于其他人来说,为什么输出缓存不适用于他们的控制台应用程序发出的请求 - 答案是cookie。

您提出的请求没有cookie,服务器的响应有新的cookie - 所以它不会缓存输出。

使用此答案中的代码发出请求,所有内容都会缓存正常:https://stackoverflow.com/a/3892075

我必须做的是向我的应用程序的主页发出请求。这会使会话排序,并返回会话cookie。然后将这些cookie添加到后续请求中(到我想要缓存的页面),一切都按预期工作。

相关问题