Response.Redirect in ASP.NET AJAX calls

*爱你&永不变心* 提交于 2019-12-01 10:37:55

问题


I have a button in an UpdatePanel that if it is clicked will redirect the user to another page in the same folder in some cases and otherwise will update the UpdatePanel with some info. If I do a redirect in this way:

Response.Redirect("Test.aspx");

it does a redirect to /Test.aspx which in most cases would be fine, but the problem is that the application is accessed through a reverse proxy (at x.com/y/) which will cause some problem since /Test.aspx will redirect the user to a nonexisting file in the root of the server that does the proxying.

Is it possible to force the redirect to skip the / stuff since it is not necessary in this case, since both files are in the same folder.

Edit: Code sample

<asp:ScriptManager ID="script" runat="server" />
<asp:UpdateProgress ID="prog" runat="server" AssociatedUpdatePanelID="up">
   <ProgressTemplate>
      <h1>Waiting...</h1>
   </ProgressTemplate>
</asp:UpdateProgress>
<asp:UpdatePanel ID="up" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="true">
   <ContentTemplate>
       <asp:TextBox ID="txt" runat="server" />
           <asp:Button ID="btn" runat="server" OnClick="click" Text="Button" />
    </ContentTemplate>        
</asp:UpdatePanel>

Click method:

protected void click(object sender, EventArgs e)
{
  Thread.Sleep(3000);
  if (txt.Text == "redirect")
     Response.Redirect("Test.aspx");
  else
     txt.Text = "";
}

回答1:


Did you give a try for redirecting to a relative path? Something like below?

Response.Redirect("~/Test.aspx") 

Please give the correct relative path where the Test.aspx resides. Hope this helps.




回答2:


You can't redirect in an async postback. Add the button as a PostBackTrigger:

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <Triggers>
        <asp:PostBackTrigger ControlID="Button1" />
    </Triggers>
    <ContentTemplate>

    </ContentTemplate>
</asp:UpdatePanel>   

The other solution is to add the following script module to your web.config:

<httpModules>
    <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</httpModules>



回答3:


in your ajax callback handler you should check 301 status code and redirect like below

response = ajaxContext.get_response();
if (response.get_statusCode() == 301)
  window.location = response.getResponseHeader('Location');


来源:https://stackoverflow.com/questions/8078641/response-redirect-in-asp-net-ajax-calls

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!