Configuring client caching using static OWIN files

This is my Startup.cs, where I link my index page to the '/ app' route.

... using Microsoft.Owin.FileSystems; using Microsoft.Owin.StaticFiles; using Microsoft.Owin.Diagnostics; [assembly: OwinStartup(typeof(conApi.Startup))] namespace conApi { public class Startup { public void Configuration(IAppBuilder app) { ////Set static files ConfigureFiles(app); //Enable Cors app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll); } public void ConfigureFiles(IAppBuilder app) { app.Map("/app", spa => { spa.Use((context, next) => { context.Request.Path = new PathString("/index.html"); return next(); }); spa.UseStaticFiles(); }); } } } 

It works like a charm, but I don’t know how to configure client caching. I would like to know how to set the Expires header if this is possible when using static OWIN files?

DECISION
Tratcher provided a link to the documentation of the StaticFilesOptions classes, etc., which led me to the solution. Added StaticFilesOptions method to ConfigureFiles method, for example:

 public void ConfigureFiles(IAppBuilder app) { var staticFileOptions = new StaticFileOptions { OnPrepareResponse = (StaticFileResponseContext) => { StaticFileResponseContext.OwinContext.Response.Headers.Add("Cache-Control",new[] { "public", "max-age=1000" }); } }; app.Map("/app", spa => { spa.Use((context, next) => { context.Request.Path = new PathString("/index.html"); return next(); }); spa.UseStaticFiles(staticFileOptions); }); } 
+7
caching owin
source share
1 answer

You can pass StaticFilesOptions to UseStaticFiles. In the settings, the OnPrepareResponse event is used to configure your answers. See http://katanaproject.codeplex.com/SourceControl/latest#src/Microsoft.Owin.StaticFiles/StaticFileOptions.cs

+3
source share

All Articles