简体   繁体   English

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

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

I wrote a middleware for resize image in asp.net core.我在 asp.net 内核中编写了一个用于调整图像大小的中间件。 but this middleware only works if browser has been not cached.但是这个中间件只有在浏览器没有被缓存的情况下才有效。 first call middleware is ok but from second call, the request does not enter middleware.第一次调用中间件没问题,但从第二次调用开始,请求没有进入中间件。 after clear cache in browser(ctrl+f5) the request enters middleware.在浏览器中清除缓存(ctrl+f5)后,请求进入中间件。 How is this problem solved?这个问题是如何解决的?

stratup.cs层级文件

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

        }
    }
}

You can simply add some versioning to your image files, like:您可以简单地为您的图像文件添加一些版本控制,例如:

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

so as the file path is different, browser will not get it from cache.所以由于文件路径不同,浏览器不会从缓存中获取它。 but please be aware that all images (that have this kind of versioning) will be downloaded every time from server effecting page load time and server load.但请注意,每次都会从服务器下载所有图像(具有这种版本控制),从而影响页面加载时间和服务器负载。

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

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