I created a new solution from WebApi .Net Core 2.0 template in Visual Studio. I added the following in startup.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddHttpCacheHeaders(opt => opt.MaxAge = 600);
services.AddResponseCaching();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseResponseCaching();
app.UseHttpCacheHeaders();
app.UseMvc();
}
Then, with postman, I hit http://localhost:xxxx/api/values
which is a endpoint created by the template and that returns ["value1","value2"]
Note that I made sure that Postman doesn't send no-cache header (in Postman settings).
The HttCacheHeaders
service comes from that repo. It adds HTTP Cache Headers. So my endpoint response header is:
- Cache-Control: public,max-age=600
- Content-Type: application/json; charset=utf-8
- Date: Fri, 29 Sep 2017 14:02:29 GMT
- ETag: C5DFA8974BB722D27E71EE50D3D14625
- Expires: Fri, 29 Sep 2017 14:03:29 GMT
- Last-Modified: Fri, 29 Sep 2017 14:02:29 GMT
- Server: Kestrel
- Transfer-Encoding: chunked
- Vary: Accept, Accept-Language, Accept-Encoding
- X-Powered-By: ASP.NET
- X-SourceFiles: =?UTF-8?B?................
The problem is that nothing gets cached. the Ouput
windows only shows The response could not be cached for this request.
So I'm a bit lost on how to use ASP.NET Core ResponseCaching Middleware
.
Update
If I don't use the HttCacheHeaders
service but add [ResponseCache(Duration = 600)]
to the action of my controller, the cache works.
Note that reason that I want to use HttCacheHeaders
is for ETag
and Last-Modified
to later do Validation Caching as well as Expiration Caching.