繁体   English   中英

asp.net 内核中的中间件仅在清除缓存的情况下工作

[英]Middleware in asp.net core work only with clear cache

我在 asp.net 内核中编写了一个用于调整图像大小的中间件。 但是这个中间件只有在浏览器没有被缓存的情况下才有效。 第一次调用中间件没问题,但从第二次调用开始,请求没有进入中间件。 在浏览器中清除缓存(ctrl+f5)后,请求进入中间件。 这个问题是如何解决的?

层级文件

namespace ImageResizer
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {


            var builder = new ConfigurationBuilder().AddJsonFile("appsettings.json");
            Configuration = builder.Build();
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllersWithViews();
            services.Configure<SiteSettings>(Configuration.GetSection(nameof(SiteSettings)));
            services.AddImageResizer();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            app.UseImageResizer();
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }
            //app.UseStaticFiles();
            var pathImage = Configuration.GetSection("SiteSettings").Get<SiteSettings>().ImageLocation;

            app.UseStaticFiles(new StaticFileOptions
            {
                FileProvider =  new PhysicalFileProvider(Path.Combine(pathImage)),
                RequestPath = "/StaticFiles"
            });
            
            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
            });

        }
    }
}

您可以简单地为您的图像文件添加一些版本控制,例如:

<img src="filename.png?v=@Guid.NewGuid().ToString()" />

所以由于文件路径不同,浏览器不会从缓存中获取它。 但请注意,每次都会从服务器下载所有图像(具有这种版本控制),从而影响页面加载时间和服务器负载。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM