Toshi Kishino
Toshi Kishino

Reputation: 31

How to implement cache for images using .net core?

I want to cache my images and found something like this

<cache enabled="true"> Current Time Inside Cache Tag Helper: @DateTime.Now </cache>

https://learn.microsoft.com/en-us/aspnet/core/mvc/views/tag-helpers/built-in/cache-tag-helper?view=aspnetcore-3.1 How can I implement my image on this?

Upvotes: 1

Views: 4078

Answers (1)

Saeid Babaei
Saeid Babaei

Reputation: 481

Caching for images is client cache and you should configure your app like below:

app.UseStaticFiles(new StaticFileOptions
            {
                ServeUnknownFileTypes = false,
                OnPrepareResponse = ctx =>
                {
                    const int durationInSeconds = 60 * 60 * 24;
                    ctx.Context.Response.Headers[HeaderNames.CacheControl] = "public,max-age=" + durationInSeconds;
                    ctx.Context.Response.Headers[HeaderNames.Expires] = new[] { DateTime.UtcNow.AddYears(1).ToString("R") }; // Format RFC1123
                }
            });

Upvotes: 1

Related Questions