2

My output caching doesn't appear to be working as expected This is a controller method to return the default page for my website. I have cached it for 40secs (test -> in live it is cached for much longer)

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

I have 2 problems

First

When I hit the endpoint for the first time it is cached correctly on the server and returns the following response header (as expected) among others.

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

When I open a new browser tab and paste in the same endpoint after 5 seconds. it goes to the server and returns a 200 with content and the following header

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

This does not seem correct to me. I expected it to send a request to the server with the an If-modified-since header. If the cached response was rebuilt since the time in the header then it would return a 200 + content else a 304. So in this case as the server cache was not rebuilt it should return a 304. Also as the max-age is so large it is now invalid and thus is immediately regarded as stale. So what am I doing wrong...?

Secondly

I put a breakpoint in my Index method and hit the endpoint from my browser. On the first time I hit the Index endpoint the breakpoint is hit. Subsequent requests to the endpoint do not hit the breakpoint for the next 40 seconds as expected. However if I add a "/" to the end of my endpoint in the browser it will hit the breakpoint ignoring the cache. How do I avoid this ..?

martin
  • 570
  • 3
  • 8
  • 20
  • Regarding the second half of your question, the URL 'http://www.example.com' and 'http://www.example.com/', with the /, are different pages. You'll need to have a URL rewriting module in Web.Config if you want both to be treated the same. I'll modify my answer below. – kodikas Feb 18 '17 at 17:32

1 Answers1

0

I just had this same problem. There's a thread on github here (I think it's a bug): https://github.com/Microsoft/dotnet/issues/330

The solution for me was as follows: (1) Download and install the .Net 4.6.2 Dev Framework pack. Go to the Properties page of your project, and either select it form the list or go to Install Other Frameworks... (2) Target your app to 4.6.2. You'll need to restart the project (it has a message). (3) Go to web.config and ensure these values are set:

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

(4) Finally, go to the NuGet Package Installer and install the following package (search for it): Microsoft.AspNet.OutputCache.OutputCacheModuleAsync

The problem was fixed for me. I hope this helps.

Question 2: Removing trailing slash

Add this code in web.config in the proper section:

<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" />

kodikas
  • 387
  • 1
  • 5
  • 12