问题
I am trying to use ImageSharp.Web to resize images through querystring parameters eg: http://localhost:5001/content/photo/img.jpg?width=800&height=600
I created a new MVC Asp.Net Core 3.1 project, installed the packages and followed the Documentation
I tried the minimum configuration described and several variations, but it seems that the middleware is not intercepting the image request. I always get the original image and no error is triggered.
Am I missing something? What would be the minimum possible configuration for this feature?
Tks!
Startup.cs :
public void ConfigureServices(IServiceCollection services)
{
services.AddDependencyInjectionSetup();
services.AddControllersWithViews();
//https://docs.sixlabors.com/articles/imagesharp.web/gettingstarted.html
services.AddImageSharp();
services.AddImageSharpCore(
options =>
{
options.MaxBrowserCacheDays = 7;
options.MaxCacheDays = 365;
options.CachedNameLength = 8;
options.OnParseCommands = _ => { };
options.OnBeforeSave = _ => { };
options.OnProcessed = _ => { };
options.OnPrepareResponse = _ => { };
})
.SetRequestParser<QueryCollectionRequestParser>()
.SetMemoryAllocator(provider => ArrayPoolMemoryAllocator.CreateWithMinimalPooling())
.Configure<PhysicalFileSystemCacheOptions>(options =>
{
options.CacheFolder = "imagesharp-cache";
})
.SetCache<PhysicalFileSystemCache>()
.SetCacheHash<CacheHash>()
.AddProvider<PhysicalFileSystemProvider>()
.AddProcessor<ResizeWebProcessor>()
.AddProcessor<FormatWebProcessor>();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
app.UseImageSharp();
}
回答1:
The call to
app.UseImageSharp();
must be before
app.UseStaticFiles();
What is happening is the built in Static Files middle-ware is kicking in before its even hitting the ImageSharp one.. the order you register items in the Configure()
section of startup.cs is important as that's they order they run in.
来源:https://stackoverflow.com/questions/61738671/imagesharp-web-resize-image-by-querystring-in-asp-net-core-3-1