DZone
Web Dev Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Web Dev Zone > Add HTTP Headers to Static Files in ASP.NET Core

Add HTTP Headers to Static Files in ASP.NET Core

In this article we have a look at how to modify the headers in static files in ASP.NET Core. Sometimes you just have to disable a cache!

Juergen Gutsch user avatar by
Juergen Gutsch
·
Aug. 07, 16 · Web Dev Zone · Tutorial
Like (2)
Save
Tweet
4.65K Views

Join the DZone community and get the full member experience.

Join For Free

Usually, static files like JavaScript, CSS, images, and so on, are cached on the client after the first request. But sometimes, you need to disable the cache or to add a special cache handling.

To provide static files in an ASP.NET Core application, you use the StaticFileMiddleware:

app.UseStaticFiles();


This extension method has two overloads. One of them needs a StaticFileOptions instance, which is our friend in this case. This option has a property called OnPrepareResponse of type Action<StaticFileResponseContext>. Inside this action, you have access to the HttpContext and many more. Let's see how it looks like to set the cache lifetime to 12 hours:

app.UseStaticFiles(new StaticFileOptions()
{
    OnPrepareResponse = context =>
    {
        context.Context.Response.Headers["Cache-Control"] = 
                "private, max-age=43200";

        context.Context.Response.Headers["Expires"] = 
                DateTime.UtcNow.AddHours(12).ToString("R");
    }
});


With the StaticFileResponseContext, you also have access to the file of the currently handled file. With this info, it is possible to manipulate the HTTP headers just for a specific file or file type.

This approach ensures that the client doesn't use pretty much outdated files, but uses cached versions while working with it. We use this in an ASP.NET Core single page application, which uses many JavaScript and HTML template files. In combination with continuous deployment, we need to ensure the application uses the latest files.

ASP.NET ASP.NET Core

Published at DZone with permission of Juergen Gutsch, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Kubernetes Data Simplicity: Getting Started With K8ssandra
  • Why "Polyglot Programming" or "Do It Yourself Programming Languages" or "Language Oriented Programming" sucks?
  • Event Loop in JavaScript
  • Common Mistakes to Avoid When Migrating

Comments

Web Dev Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo