0

我正在尝试使用 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();


    }
4

1 回答 1

4

呼吁

    app.UseImageSharp();

必须在之前

    app.UseStaticFiles();

正在发生的事情是内置的静态文件中间件甚至在它到达 ImageSharp 之前就已经启动了。您在Configure()startup.cs 部分中注册项目的顺序很重要,因为这是它们运行的​​顺序。

于 2020-05-12T12:24:49.810 回答