【问题标题】:outputcache a site from console app从控制台应用程序输出缓存站点
【发布时间】:2013-02-28 15:21:03
【问题描述】:

我想运行一个控制台应用程序,该应用程序在所有输出缓存页面上调用网站,以便为用户缓存所有缓存的页面。使用浏览器时,页面缓存正确,但我希望能够运行一个为用户缓存所有页面的功能。

我正在使用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>

【问题讨论】:

  • 你用的是什么缓存?
  • 使用costum 制作的outputcache 属性标记控制器方法。

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


【解决方案1】:

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

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

使用此答案中的代码提出您的请求,一切都会缓存好:https://stackoverflow.com/a/3892075

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

【讨论】:

    猜你喜欢
    • 2015-11-07
    • 1970-01-01
    • 2011-11-27
    • 2011-03-10
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 2010-12-22
    • 2010-09-16
    相关资源
    最近更新 更多