【问题标题】:OutputCache not working correctly -> ASP.NET MVC5OutputCache 无法正常工作-> ASP.NET MVC5
【发布时间】:2017-02-17 16:21:28
【问题描述】:

我的输出缓存似乎没有按预期工作 这是返回我网站的默认页面的控制器方法。 我已经将它缓存了 40 秒(测试 -> 在 live 中它被缓存了更长的时间)

[OutputCache(Duration = 40, Location = OutputCacheLocation.ServerAndClient, VaryByParam = "none")]
public async Task<ActionResult> Index(){
......
}

我有两个问题

第一

当我第一次点击端点时,它会正确缓存在服务器上并返回以下响应标头(如预期的那样)。

status:200
Cache-Control:"private, max-age=40, s-maxage=0"

当我打开一个新的浏览器选项卡并在 5 秒后粘贴到同一端点时。它转到服务器并返回 200 内容和以下标头

status:200
Cache-Control:"private, max-age=63622944145, s-maxage=63622944105"

这对我来说似乎不正确。我希望它使用 If-modified-since 标头向服务器发送请求。如果缓存的响应自标头中的时间起重建,则它将返回 200 + 内容,否则返回 304。因此在这种情况下,由于未重建服务器缓存,它应该返回 304。另外,由于 max-age 如此之大它现在无效,因此立即被视为陈旧。 那我做错了什么...?

其次

我在我的 Index 方法中放置了一个断点,并从我的浏览器中点击了端点。在我第一次命中索引端点时,断点被命中。对端点的后续请求在接下来的 40 秒内没有按预期命中断点。但是,如果我在浏览器中的端点末尾添加一个“/”,它将命中断点而忽略缓存。 我该如何避免这种情况..?

【问题讨论】:

  • 关于你问题的后半部分,URL 'example.com' 和'example.com' 加上 / 是不同的页面。如果您希望两者被同等对待,则需要在 Web.Config 中有一个 URL 重写模块。我将在下面修改我的答案。

标签: asp.net-mvc caching outputcache http-caching


【解决方案1】:

我也遇到了同样的问题。 github上有一个线程here(我认为这是一个错误): https://github.com/Microsoft/dotnet/issues/330

我的解决方案如下: (1) 下载并安装 .Net 4.6.2 Dev Framework 包。转到项目的 Properties 页面,然后从列表中选择它或转到 Install Other Frameworks... (2) 将您的应用程序定位到 4.6.2。您需要重新启动项目(它有一条消息)。 (3) 转到 web.config 并确保设置了这些值:

<system.web>
<compilation targetFramework="4.6.2" />
<httpRuntime targetFramework="4.6.2" />

(4)最后,进入NuGet Package Installer,安装以下包(搜索):Microsoft.AspNet.OutputCache.OutputCacheModuleAsync

问题已为我解决。我希望这会有所帮助。

问题 2:删除尾部斜杠

在 web.config 的适当部分添加此代码:

<system.webServer>
<rewrite>
  <rules>
    <rule name="Remove trailing slash" enabled="true" stopProcessing="true">
      <match url="(.*)/$" />
      <conditions>
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
      </conditions>
      <action type="Redirect" redirectType="Permanent" url="{R:1}" />
    </rule>
  </rules>
</rewrite>
<validation validateIntegratedModeConfiguration="false" />

【讨论】:

    猜你喜欢
    • 2023-01-23
    • 2013-11-16
    • 2013-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-09
    • 1970-01-01
    相关资源
    最近更新 更多