Change the headers of static files in Asp.net Core

后端 未结 5 1657
误落风尘
误落风尘 2020-12-06 05:47

I am using package Microsoft.AspNet.StaticFiles and configuring it in Startup.cs as app.UseStaticFiles(). How can I change the headers

5条回答
  •  执笔经年
    2020-12-06 06:38

    Based on Josh Mouch's answer above, added code to determine if it's a pdf file

    Startup.cs:

          app.UseStaticFiles(new StaticFileOptions
          {
            OnPrepareResponse = ctx =>
              {
                if(ctx.File.Name.ToLower().EndsWith(".pdf"))
                {
                  ctx.Context.Response.Headers.Append("Cache-Control", "public,max-age=86400");
                }
                else
                {
                  ctx.Context.Response.Headers.Append("Cache-Control", "public,max-age=31104000");
                }
              }
          });
    

提交回复
热议问题