Response.Redirect对目标URL进行了不正确的URL编码

时间:2014-04-14 18:37:18

标签: asp.net vb.net response.redirect

在旧的VB.NET网站中,管理部分中有一个页面,其中包含http://example.com/Admin/EditUser.aspx等网址。在该页面上的提交按钮的事件处理程序结束时,它会尝试使用添加的查询字符串重定向到自身,如下所示:Response.Redirect("EditUser.aspx?a=b")。这会导致我的浏览器尝试重定向到http://example.com/Admin/%2fAdmin%2fEditUser.aspx%3fa%3db,这是错误的。如果我将~/添加到Response.Redirect调用,它会重定向到http://example.com/Admin/%2fEditUser.aspx%3fa%3db,这仍然是错误的。即使我从重定向网址中删除了查询字符串,Response.Redirect仍会添加多余的%2f

造成这种情况的原因是什么,我该如何解决?

编辑:这家伙有同样的问题并得出结论它是.NET 4.0中的一个错误:Response.redirect puts extra characters in the url Error 400

他的解决方法涉及在Javascript中使用document.location。但我仍然想知道如何在服务器端解决这个问题。

1 个答案:

答案 0 :(得分:1)

这是Response.Redirect中的错误,该错误是由UpdatePanel中包含的提交按钮引起的。解决方案是向<Triggers>添加UpdatePanel部分,如下所示:

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <Triggers>
        <asp:PostBackTrigger ControlID="btnSubmit" />  
    </Triggers>
    <ContentTemplate>
        <%-- ...Content goes here... --%>
        <asp:Button runat="server" ID="btnSubmit"/>
    </ContentTemplate>
</asp:UpdatePanel>

这让我疯了,所以我希望它可以帮助别人。