1

I'm trying to resize images using Imageflow's query string API in my .net core application. I have installed Imageflow.Server. My Configure Method looks like this:

public void Configure(IApplicationBuilder App, IWebHostEnvironment Env)
    {
        if (AppSettings.IsDevelopment)
        {
            App.UseDeveloperExceptionPage();
        }
        else
        {
            App.UseExceptionHandler(Options =>
            {
                App.UseExceptionHandler("/error/404/");

            });
            App.UseHsts();
        }

        App.UseImageflow(new ImageflowMiddlewareOptions()
            .SetMapWebRoot(false)
            .MapPath("/upload/", "{upload folder path}"));

        App.UseFileServer();
        App.UseRouting();
        App.UseSession();

        App.UseEndpoints(Endpoints =>
        {
            Endpoints.MapControllers();
        });

    }

There is no problem on localhost or if the upload folder is inside the wwwroot folder, but if upload folder is outside the app root directory then images won't resize. Any Ideas how can I solve this problem?

2 Answers2

1

If you have registered the folder as a virtual directory, IIS will prevent ASP.NET Core from serving or resizing those files.

Unmap the virtual directory in IIS and use ASP.NET Core instead.

To allow ASP.NET Core to serve files, call UseStaticFiles a second time to map that virtual directory: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/static-files?view=aspnetcore-3.1

You'll also still want to call MapPath on ImageflowMiddlewareOptions for the same virtual directory.

Lilith River
  • 16,204
  • 2
  • 44
  • 76
  • When I only want to serve images from a former virtual directory, I only need the MapPath() call in the ImageflowMiddlewareOptions and don't need to add the second UseStaticFiles() in Startup.cs at all. It ends up being a pretty elegant solution. – Tommy Williams Mar 15 '22 at 18:00
0

This could be a permissions issue. If on-disk permissions do not allow the user account running the app to access the folder, it will fail.

Lilith River
  • 16,204
  • 2
  • 44
  • 76
  • App is accessing the folder and all images are shown on website, but with original size. Maybe it needs some special permissions? I have granted permissions for IUSR, SYSTEM, NETWORK SERVICE, IIS_IUSRS, Administrators and Users. – Kote Didebulidze Jul 27 '20 at 14:18
  • If images are being served from /upload/ then there's no reason for commands to not work. Can you share some example URLs that don't work? – Lilith River Jul 27 '20 at 20:44
  • Sure. https://ogpgeorgia.gov.ge/en/ I'm trying to resize slider images and News images. – Kote Didebulidze Jul 28 '20 at 07:16
  • Is the upload folder inside wwwroot? The file server may be taking over the requests. Try swapping the order of the middleware calls. – Lilith River Jul 29 '20 at 16:44
  • upload folder is outside wwwroot and swapping the order of the middleware calls didn't help. – Kote Didebulidze Jul 31 '20 at 08:53
  • Could you zip up your project and send it to support@imazen.io? Something odd is going on. – Lilith River Aug 03 '20 at 20:12
  • I made a sample project and sent it on email. Thanks for your time. – Kote Didebulidze Aug 07 '20 at 10:02
  • I was unable to reproduce the problem, even when moving the upload folder outside of wwwroot. What OS and web server are you seeing the problem on? – Lilith River Aug 07 '20 at 17:23
  • windows server 2016 standard, IIS web server – Kote Didebulidze Aug 07 '20 at 19:02
  • I found the problem. In IIS I have registered same virtual directory. I removed it and now all images are resized but now I can't reach other files from same folder. Is there any way I can make it work with registered virtual directory? – Kote Didebulidze Aug 17 '20 at 07:45
  • 1
    You can call UseStaticFiles a second time to map that virtual directory in ASP.NET Core: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/static-files?view=aspnetcore-3.1 – Lilith River Aug 18 '20 at 15:06
  • It works. Thank you very much. Can you write this as answer so I could accept it as solution? – Kote Didebulidze Aug 20 '20 at 07:14