ImageSharp.Web - 在 Asp.Net Core 3.1 中通过查询字符串调整图像大小

ImageSharp.Web - resize image by querystring in Asp.Net Core 3.1

我正在尝试使用 ImageSharp.Web 通过查询字符串参数调整图像大小,例如:http://localhost:5001/content/photo/img.jpg?width=800&height=600

我创建了一个新的 MVC Asp.Net Core 3.1 项目,安装了软件包并遵循了 Documentation

我尝试了描述的最低配置和几种变体,但中间件似乎没有拦截图像请求。我总是得到原始图像并且没有触发错误。

我错过了什么吗? 此功能的最低可能配置是什么?

感谢!

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();


    }

的调用
    app.UseImageSharp();

必须在

之前
    app.UseStaticFiles();

正在发生的事情是内置的静态文件中间件甚至在它到达 ImageSharp 中间件之前就开始运行了。在 startup.cs 的 Configure() 部分中注册项目的顺序很重要因为这是他们订购的 运行 in.