首页 > 解决方案 > 如何使用.net core 实现图像缓存?

问题描述

我想缓存我的图像并找到类似的东西

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

https://docs.microsoft.com/en-us/aspnet/core/mvc/views/tag-helpers/built-in/cache-tag-helper?view=aspnetcore-3.1 如何在此实现我的图像?

标签: c#asp.net-corecaching

解决方案


图片缓存是客户端缓存,您应该像下面这样配置您的应用程序:

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
                }
            });

推荐阅读