[英]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.