0

I would like to cache one of controllers action (which result is dynamically rendered javascript code), so i have added this annotations:

/// <summary>
/// Renders embed client javascript.
/// </summary>
/// <param name="minify">True for minifing script</param>
/// <returns>Javascript result.</returns>
[HttpGet]
[ActionName("xtech.embed.client.js")]
[OutputCache(Duration = 3600, VaryByParam = "minify")]
public ActionResult ClientScript(bool minify = false)

On the first request i get response headers:

cache-control:public, max-age=1812
content-length:23336
content-type:application/x-javascript; charset=utf-8
date:Tue, 06 Mar 2018 13:12:50 GMT
expires:Tue, 06 Mar 2018 13:43:03 GMT
last-modified:Tue, 06 Mar 2018 12:43:03 GMT
server:Microsoft-IIS/10.0
status:200
vary:*

Second run goes with response header:

cache-control:public, max-age=1670
date:Tue, 06 Mar 2018 13:15:11 GMT
expires:Tue, 06 Mar 2018 13:43:03 GMT
last-modified:Tue, 06 Mar 2018 12:43:03 GMT
server:Microsoft-IIS/10.0
status:304
vary:*

This means that each subsequent request browser queries server for resource modification. I would like to change this behavior so that resource would be restored from browser cache without query the server till cache expiration.

Do you know if this can be done with data attributes or requires custom cache options and some extra code?

Note: Cache configuration (inside webconfig) for static files like: How to specify HTTP expiration header? (ASP.NET MVC+IIS) cannot be used because result is dynamic (view rendered javascript).

IT Man
  • 933
  • 3
  • 12
  • 33
  • 1
    Thats not what output caching does. Output caching stores the resulting HTML from the action on the server, not the client. It doesn't alter the expires HTTP header – Liam Mar 06 '18 at 13:32
  • Possible duplicate of [How to specify HTTP expiration header? (ASP.NET MVC+IIS)](https://stackoverflow.com/questions/2608739/how-to-specify-http-expiration-header-asp-net-mvciis) – Liam Mar 06 '18 at 13:33
  • @Liam its a bit different case, it refers to static file caching. – IT Man Mar 06 '18 at 13:45
  • If you want to specify a expires header in .Net the mechanism is the same [`Response.Cache.SetExpires(DateTime.Now.AddYears(1));`](https://stackoverflow.com/a/6443000/542251). It's really not clear what your trying to achieve or why this doesn't address your issue? `OutputCache` is a mechanism in MVC to cache the result of a Razor view on the server. It does not add any HTTP headers – Liam Mar 06 '18 at 13:53
  • @Liam i cannot use this kind of configuration because this is not static content (file does not physically inside folder - it is generated inside view). – IT Man Mar 06 '18 at 13:53
  • [`Response.Cache.SetExpires(DateTime.Now.AddYears(1));`](https://stackoverflow.com/a/6443000/542251)??? you may need to write a custom HTTP handler but you still just set the header in code on the `Response` object. – Liam Mar 06 '18 at 13:54
  • @Liam no, answer: webconfig. I'm chcecking what happens when i set response cache as you pointed... – IT Man Mar 06 '18 at 13:56
  • 1
    @Liam, worked! Thanks for help and patience. But also works with data attributes [OutputCache(Duration = 3600, Location = OutputCacheLocation.Client)], it corresponds to cache-control:private – IT Man Mar 06 '18 at 14:07

0 Answers0