1

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?

5
  • 1
    Does this answer your question? How to cache css,js or images files to asp.net core Commented Dec 9, 2019 at 14:57
  • You can give it a try with this answer Commented Dec 9, 2019 at 15:02
  • 1
    Your question is unclear. The cache tag helper is for caching bits of HTML (presumably generated by Razor code where the compilation times would be higher than desired). It's not going to cause an actual image file to be cached client-side; that's not what it's for. For that, you need to set Cache-Control headers on the image responses. If you're serving via the static files middleware, you can configure the cache headers with that middleware. Commented Dec 9, 2019 at 15:24
  • @ChrisPratt - Loved you in Jurrasic park and Guardians of the Galaxy. Keep up the good work! Commented Dec 9, 2019 at 16:12
  • 1
    @TraeMoore LMFAO, how many times a day you think Chris hears that? (I went to school with 2 mike jones lol) Commented Mar 25, 2022 at 20:12

1 Answer 1

1

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
                }
            });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.