我正在尝试使用ImageSharp.Web通过查询字符串参数调整图像大小,例如:http://localhost:5001/content/photo/img.jpg?width=800&height=600
我创建了一个新的 MVC Asp.Net Core 3.1 项目,安装了软件包并遵循了文档
我尝试了描述的最低配置和几种变体,但似乎中间件没有拦截图像请求。我总是得到原始图像,并且没有触发错误。
我错过了什么吗? 此功能的最低可能配置是什么?
啧啧!
启动.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();
}
对
app.UseImageSharp();
必须在之前
app.UseStaticFiles();
正在发生的事情是内置的静态文件中间件甚至在它到达ImageSharp之前就开始了......你在启动的Configure()
部分中注册项目的顺序.cs很重要,因为这是它们运行的顺序。