0

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.

1 Answer 1

0

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://learn.microsoft.com/en-us/aspnet/core/fundamentals/url-rewriting?view=aspnetcore-6.0

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.