'RewriteOptions / UseRewriter behaves inconsistently after upgrading from .net 5 to .net 6

I updated an app from .net 5 to .net 6.

This same code:

        app.UseHttpsRedirection();

        var redirectOptions = new RewriteOptions()
            .AddRedirect(@"^(?i)\bassets/collagebuilder\b/((.*)\.(gif|jpg|jpeg|png|bmp))", $"{surveyConfig.CollageBuilderAssetsRedirectUrl}/$1")
            .AddRewrite("st/(.*)", "/$1", true);

        app.UseRewriter(redirectOptions);

shows the expected images in the UI for the .net 5 version, but in .net 6 enter image description here

Error: AccountRequiresHttps.

This surveyConfig.CollageBuilderAssetsRedirectUrl is configured as "https://*****/collagebuilder" (I masked the root part of the URL).

To make matters more confusing, I have at least 2 examples where the images do show up, redirect is practically identical, and the url similarly would require https.



Solution 1:[1]

I found a solution that might help someone else who crosses this bridge.

First I created an XML file, IISUrlRewrite.xml and saved it. This part was easy for me since this is almost exactly the same that was in the project back when it was in the .net framework version's web.config:

<rewrite>
<rules>
    <rule name="images redirection">
        <match url="^assets/([0-9]+)/((.*)\.(gif|jpg|jpeg|png|bmp|mp4|mp3|ogg|wav|tiff))$" ignoreCase="true" />
        <action type="Redirect" url="https://****/assets/design/{R:1}/{R:2}" />
    </rule>
    <rule name="capture question redirection">
        <match url="^answers/([0-9]+)/([a-zA-Z0-9]+)/((.*)\.(gif|jpg|jpeg|png|bmp|mp4|mp3|ogg|wav|tiff))$" ignoreCase="true"/>
        <action type="Redirect" url="https://****/answers/design/{R:1}/{R:2}/{R:3}" />
    </rule>
    <rule name="collagebuilder redirection">
        <match url="^assets/collagebuilder/((.*)\.(gif|jpg|jpeg|png|bmp))$" ignoreCase="true" />
        <action type="Redirect" url="https://****/collagebuilder/{R:1}" />
    </rule>
    <rule name="survey theme redirection">
        <match url="^themes/((.*)\.css)$" ignoreCase="true" />
        <action type="Redirect" url="https://****/themes/{R:1}" />
    </rule>
    <rule name="st folder redirection">
        <match url="st/(.*)" ignoreCase="true"/>
        <action type="Rewrite" url="/{R:1}" />
    </rule>
</rules>

Next, modified my Startup / Program (whatever you want) code:

app.UseHttpsRedirection();

        using (StreamReader iisUrlRewriteStreamReader =
        File.OpenText("IISUrlRewrite.xml"))
        {
            var options = new RewriteOptions()
                .AddIISUrlRewrite(iisUrlRewriteStreamReader);
            app.UseRewriter(options);
        }

        //var redirectOptions = new RewriteOptions()
        //    .AddRedirect(@"^/assets/([0-9]+)/((.*)\.(gif|jpg|jpeg|png|bmp|mp4|mp3|ogg|wav|tiff))", $"{surveyConfig.AssetsRedirectUrl}/$1/$2")
        //    .AddRedirect(@"^/answers/([0-9]+)/([a-zA-Z0-9]+)/((.*)\.(gif|jpg|jpeg|png|bmp|mp4|mp3|ogg|wav|tiff))", $"{surveyConfig.AnswerAssetsRedirectUrl}/$1/$2/$3")
        //    .AddRedirect(@"^/assets/collagebuilder/((.*)\.(gif|jpg|jpeg|png|bmp))", $"{surveyConfig.CollageBuilderAssetsRedirectUrl}/$1")
        //    .AddRedirect(@"^/themes/((.*)\.css)", $"{surveyConfig.ThemeAssetsRedirectUrl}/$1")
        //    .AddRewrite("st/(.*)", "/$1", true);


        //app.UseRewriter(redirectOptions);

I'm not sure why the original approach wasn't working for me (reliably), but this seems to do the trick. It's explained in more detail here: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/url-rewriting?view=aspnetcore-6.0

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 John