Imageprocessor: Issue with query strings on images

Created on 16 Nov 2020  路  11Comments  路  Source: JimBobSquarePants/ImageProcessor

Hello,

I've recently bought a software called Flippingbook https://flippingbook.com/ which creates HTML 5 brochures. I have published a magazine and it works fine locally.

However when I upload the magazine to my website, I am getting issues with accessing the files in the magazine and it's causing a server error. The software automaically adds query strings to the images in the magazine. Here is a link to the brochure:

https://www.s.co.uk/brochure-2/index.html

The console errors reveal the images have been set up as:

https://www.s.co.uk/brochure-2/files/assets/common/page-html5-substrates/page0001_2.jpg?uni=b5e7fd95e5123a5801a44906ede1a557

However, when you remove the query string the images show:

https://www.s.co.uk/brochure-2/files/assets/common/page-html5-substrates/page0001_2.jpg

We use the ImageProcessor on our site, and was told by the team at Flipping Book to as for your assistance.

All 11 comments

You can use the ImageProcessingModule.ValidatingRequest event to ignore those query strings.

https://imageprocessor.org/imageprocessor-web/imageprocessingmodule/#events

While the original link i posted now works adding the fix you recommend my second link https://www.medburnhomes.co.uk/brochure-2/files/assets/common/page-html5-substrates/page0001_2.jpg?uni=b5e7fd95e5123a5801a44906ede1a557 doesnt. The console suggests a Bad Request and server gives the same error as before. Is there anything else i need to add/configure

Are you seeing arg.Cancel to true? If so, don't. Just chop the parameter off the request. ImageProcessor.Web will then ignore it and the static file handler will take over

Here is my code based on your article and understanding of what you may want me to do.

In debug mode i can see all the values, I try and remove the parameter from the collection but it doesnt make a difference. I set arg.Cancel = false but also noticed it never enters this block

    protected override void OnApplicationStarted(object sender, EventArgs e)
    {
        base.OnApplicationStarted(sender, e);
        ImageProcessingModule.ValidatingRequest += ImageProcessingModule_ValidatingRequest;
    }



    private void ImageProcessingModule_ValidatingRequest(object sender, ImageProcessor.Web.Helpers.ValidatingRequestEventArgs args)
    {
        if (!string.IsNullOrWhiteSpace(args.QueryString))
        {
            var queryCollection = HttpUtility.ParseQueryString(args.QueryString);
            queryCollection.Remove("uni");
            if (queryCollection.AllKeys.Contains("alpha"))
            {
                args.Cancel = false;
                return;
            }



            // If there's a crop parameter, force it to always just be a specific value
            if (queryCollection.AllKeys.Contains("crop"))
            {
                queryCollection["crop"] = "10,10,10,10";



                // This performs the reverse of ParseQueryString since the result of ParseQueryString
                // is actually an instance of System.Web.HttpValueCollection
                args.QueryString = queryCollection.ToString();
            }
        }
    }

Could you confirm what the final code should be?

Maybe a better approch would be to completely remove the ImageProcessorModule within the directory contains the static flipbook files?

This can be done by adding the following configuation into brochure-2\Web.config (the httpModules section might be redundant, as that's for very old IIS versions):

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.web>
        <httpModules>
            <remove name="ImageProcessorModule" />
        </httpModules>
    </system.web>
    <system.webServer>
        <modules>
            <remove name="ImageProcessorModule" />
        <modules>
    </system.webServer>
</configuration>

Why have you got all that extra code in your event handler?

You're never updating the args.QueryString value.

Sorry which section are you referring to? I copied the code from the link but can you confirm what you need me to change to so i can test?

The code in the link is a generic example of how the event can be handled. You need to write the code to handle your specific case.

```c#
private void ImageProcessingModule_ValidatingRequest(object sender, ImageProcessor.Web.Helpers.ValidatingRequestEventArgs args)
{
if (!string.IsNullOrWhiteSpace(args.QueryString))
{
// Read
var queryCollection = HttpUtility.ParseQueryString(args.QueryString);

    // Remove
    queryCollection.Remove("uni");

    // Update
    args.QueryString = queryCollection.ToString();
}

}
```

I just now made a change to the original code which is close to what you have posted once i understood that was a boiler template code :-( - looks to be working now though i'll test a few scenarios- Thanks

Hurrah! 馃殌 Happy to help. Glad it's working

Was this page helpful?
0 / 5 - 0 ratings