简体   繁体   English

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

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

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我正在尝试使用 ImageSharp.Web 通过查询字符串参数调整图像大小,例如: 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我创建了一个新的 MVC Asp.Net Core 3.1 项目,安装了软件包并遵循了文档

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:启动.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();


    }

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM