Search code examples
asp.net-3.5asp.net-4.5response.redirect

ASP.NET: Response.Redirect() with root-relative URL (tilde, ~) repeats subfolder in path (after migrating from target framework 3.5 to 4.5)


I work on an ASP.NET web project which was migrated from target framework 3.5 to 4.5. After migration to 4.5 there is an issue with the redirecting of HTTP requests. A redirect from a called web page which is located in a subfolder causes a duplication of the root folder and subfolder in the called URL (but: no duplication with same code in 3.5). The affected subfolder is not registered as separate web application.

Hosted files are:

  • /WebAppRoot/SubfolderInWebApp/Page1.aspx
  • /WebAppRoot/SubfolderInWebApp/Page1.aspx.cs
  • /WebAppRoot/SubfolderInWebApp/Page2.aspx
  • /WebAppRoot/SubfolderInWebApp/Page2.aspx.cs

Page1 should redirect to Page2 using tilde (~) to address the root-relative path. Redirect call within /WebAppRoot/SubfolderInWebApp/Page2.aspx.cs is:

Response.Redirect("~/SubfolderInWebApp/Page2.aspx", false);

On my local machine using Microsoft Visual Studio and IIS Express the redirect points to

  • /SubfolderInWebApp/SubfolderInWebApp/Page2.aspx

On the test environment running IIS which hosts the app in WebAppRoot the redirect points to

  • /WebAppRoot/SubfolderInWebApp/WebAppRoot/SubfolderInWebApp/Page2.aspx

If I change the redirect call to

Response.Redirect("/Page2.aspx", false)

it works. But this is not really satisfying, knowing that it worked with the tilde before migration and keeping in mind that there are several other places in the application which work with the tilde (but not used in a redirect).

Here some details about my used setup:

  • local machine for development: Microsoft Visual Studio Professional 2017 15.9.49 (running IIS Express on local machine)
  • test environment: IIS 8.5.9600.16384
  • both setups use the application pool ".NET v4.5 Classic" with .NET CLR version v4.0.30319 in classic pipeline mode

I wasn't able to identify possible reasons for this behaviour yet. As far as I understand the behaviour before the migration is the expected one. But I am not very experienced with ASP.NET, so maybe I misinterpret some information or I did not use the correct keywords in my search to find a solution. Also I did not identify issues in this list of breaking changes which could be the reason for the current behaviour. But maybe I am not aware of the impact of some statements in this list.

Any idea what might be wrong in my project? Is there an obvious configuration I miss? Is my expectation of the behaviour wrong? Thanks in advance for your help.


Solution

  • I solved it with help of Leo's answer here. The relevant control was firing an asynchronous postback which caused the described behaviour above.

    When migration to 4.5 the HTTP module ScriptModule was removed from the web.config. Integrating this module again did not work for me and I wasn't aware of this. After defining the relevant control as PostBackTrigger by using the Triggers element within the UpdatePanel, it worked again:

    <asp:UpdatePanel>
        ...
        <Triggers>
            <asp:PostBackTrigger ControlID="ControlWithSyncPostback" />
        </Triggers>
    </asp:UpdatePanel>
    

    After this change everything works as expected again and there is no need to keep the ScriptModule declaration in the web.config (at least in my case).