2
votes

I wrote a middleware for resize image in asp.net core. 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. 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?}");
            });

        }
    }
}
1
you should post the code of your middleware instead. BTW it depends on when you want to run the image resizer. The answer should be exactly when the browser should NOT cache the image. So when? if you can answer that question, you will proceed closer to the answer to your problem.Hopeless

1 Answers

1
votes

You can simply add some versioning to your image files, like:

<img src="[email protected]().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.