7

In the previous version I would do this like in here. But in new version of ASP there is no web.config file, and I believe it should be done in launchSettings.json file.

Basically what I want to do it stop caching app.js file and all .html files from templates folder. How do I do it?

Community
  • 1
  • 1
Whistler
  • 1,632
  • 4
  • 24
  • 45

2 Answers2

8

Note that you're still free to add <meta> tags in your HTML pages for each page you don't want to cache:

<meta http-equiv="cache-control" content="no-cache" />

Also note that if you're deploying to IIS then you still have a wwwroot (or what you specified in project.json) where you can put a web.config files (parsed by IIS).

If you want to do it with configuration then add a Configure() method in your Startup class:

public void Configure(IApplicationBuilder application)
{
    application.Use(async (context, next) =>
    {
        context.Response.Headers.Append("Cache-Control", "no-cache");
        await next();
    });

    // ...
}

Note that if you want to apply that HTTP header only to certain pages you just need to check PathString property of HttpRequest (Request property of HttpContext) or if you need it for every static file (same as above if you want to apply to only some of them) using:

application.UseStaticFiles(new StaticFileOptions
{
    OnPrepareResponse = context =>
    {
        context.Response.Headers.Append("Cache-Control", "no-cache");
    }
};

Which headers you should send to be compatible with browsers you need to support has been discussed on Making sure a web page is not cached, across all browsers.

Community
  • 1
  • 1
Adriano Repetti
  • 60,141
  • 17
  • 127
  • 190
  • I've tried the one with the Configure, it works with Chrome, but not in IE. Have too clear cache manually to see changes – Whistler Jan 15 '16 at 14:03
  • That's another widely discussed problem. A comprehensive post here on SO: http://stackoverflow.com/q/49547/1207195 – Adriano Repetti Jan 15 '16 at 14:16
  • @Whistler: Try to use `context.Response.Headers.Append("Cache-Control", "private, max-age=0")` instead of `context.Response.Headers.Append("Cache-Control", "no-cache");` (see [the answer](http://stackoverflow.com/a/9269567/315935)). It should work in all cases inclusive proxy. – Oleg Jan 15 '16 at 14:24
  • @Whistler: Try to use `context.Response.Headers.Append("Cache-Control", "private, no-store")` instead of `context.Response.Headers.Append("Cache-Control", "no-cache");` – Oleg Jan 15 '16 at 14:31
3

If you are a PWA developer in .Net Core or you are a react or angular you can use the following code to cache all static files except the service worker or your main app.js. Thumbs up if it helps you:

 public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            //...
            //One year 31536000
            string cachePeriod = env.IsDevelopment() ? "600" : "31536000";
            app.UseStaticFiles(new StaticFileOptions{ OnPrepareResponse = ctx => {

                if (ctx.File.Name == "sw.js")
                {
                    ctx.Context.Response.Headers.Append("Cache-Control", $"public, no-cache");
                }
                else
                {
                    ctx.Context.Response.Headers.Append("Cache-Control", $"public, max-age={cachePeriod}");
                }
            } });
    //...    
}
Shadi Namrouti
  • 7,498
  • 1
  • 38
  • 47