【问题标题】:How to implement cache for images using .net core?如何使用.net core 实现图像缓存?
【发布时间】:2019-12-09 14:55:01
【问题描述】:

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

<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 如何在此上实现我的图像?

【问题讨论】:

  • 你可以试试this answer
  • 您的问题不清楚。 cache 标记助手用于缓存 HTML 位(可能由 Razor 代码生成,其中编译时间比预期的要长)。它不会导致客户端缓存实际的图像文件;那不是它的用途。为此,您需要在图像响应上设置 Cache-Control 标头。如果您通过静态文件中间件提供服务,则可以使用该中间件配置缓存标头。
  • @ChrisPratt - 在侏罗纪公园和银河护卫队中爱你。继续努力!
  • @TraeMoore LMFAO,你认为克里斯一天听到多少次? (我和 2 个 Mike Jones 一起上学,哈哈)

标签: c# asp.net-core caching


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-26
    • 2014-02-12
    • 1970-01-01
    • 2018-10-14
    • 2020-08-08
    • 1970-01-01
    • 2015-10-14
    • 2017-09-25
    相关资源
    最近更新 更多