简体   繁体   English

.NET 4.0 中的响应。重定向

[英]Response.Redirect in .NET 4.0

Response.Redirect() no longer working when upgrade the application to ASP.NET 4.0将应用程序升级到 ASP.NET 4.0 时, Response.Redirect()不再工作

  • Response.Redirect() is used inside Update panel Response.Redirect()在更新面板中使用
  • and we using the AjaxToolKit 4.0我们使用AjaxToolKit 4.0

it gives me the error:它给了我错误:

Error: Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed.错误:Sys.WebForms.PageRequestManagerParserErrorException:无法解析从服务器接收到的消息。 Common causes for this error are when the response is modified by calls to Response.Write(), response filters, HttpModules, or server trace is enabled.此错误的常见原因是通过调用 Response.Write()、响应过滤器、HttpModules 或启用了服务器跟踪来修改响应。 Details: Error parsing near详细信息:附近解析错误

you have to do this你必须这样做

string redirectURL=(a proper url goes here)

string script = "window.location='" + redirectURL + "';";

ScriptManager.RegisterStartupScript(this, typeof(Page), "RedirectTo", script, true);

Try passing True as the second argument like this:尝试将True作为第二个参数传递,如下所示:

Response.Redirect("http://...", true);

Had the same issue... You need to replace your version of the AjaxControlToolkit with the latest version built specifically for 4.0.有同样的问题...您需要用专门为 4.0 构建的最新版本替换您的 AjaxControlToolkit 版本。 It's a drop-in replacement, so it should affect anything else.这是一个直接替代品,所以它应该会影响其他任何东西。 See Ajaxcontroltoolkit on codeplex请参阅 codeplex 上的 Ajaxcontroltoolkit

We had the same issue.我们有同样的问题。 Resolved by using PostBackTrigger control使用 PostBackTrigger 控件解决

<Triggers>
   <asp:PostBackTrigger ControlID="UploadButton" />
</Triggers>

http://msdn.microsoft.com/en-us/library/system.web.ui.postbacktrigger.aspx http://msdn.microsoft.com/en-us/library/system.web.ui.postbacktrigger.aspx

I have been choking with this problem for entire days without advancing.我一整天都在为这个问题而窒息而没有进步。 Update contents of a panel, AsyncPostBackTrigger if the caller is outside updatePanel.更新面板的内容,如果调用者在 updatePanel 之外,则为 AsyncPostBackTrigger。 If you want to redirect, you add it as PostBackTrigger.如果要重定向,请将其添加为 PostBackTrigger。 Both things on updatepanel using: updatepanel 上的两件事都使用:

<asp:UpdatePanel ID="myUpdatePanel" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:LinkButton ID="link" runat="server" OnClick="link_Click"/>
    </ContentTemplate>
    <Triggers>
        <asp:PostBackTrigger ControlID="link" />
    </Triggers>
</asp:UpdatePanel>

My issue had to do with the controls being generated dinamically inside a custom control due a listview.我的问题与由于列表视图而在自定义控件中动态生成的控件有关。

The solution:解决方案:

ScriptManager.GetCurrent(this.Page).RegisterPostBackControl(myRedirectButton); ScriptManager.GetCurrent(this.Page).RegisterPostBackControl(myRedirectButton);

In my case, i was using a listview on the item data bound, so i got the control using:就我而言,我在项目数据绑定上使用了一个列表视图,所以我使用以下方法获得了控制:

Control myRedirectButton = ListViewDataItem.GetControl("controlId")

Keep in mind the diference between Async and not ones.请记住异步和非异步之间的区别。 It was the main point that i didnt notice it until very far.这是我直到很远才注意到它的要点。

You are trying to redirect to another page with an async request.您正在尝试使用异步请求重定向到另一个页面。

You can overload the Response.Redirect function and set it to false.您可以重载Response.Redirect function 并将其设置为 false。

Response.Redirect("URL",false);

by setting it to false, it will terminate your current request and go to the next request.通过将其设置为 false,它将终止您当前的请求,并将 go 终止到下一个请求。

I am 100% sure it will work for you.我 100% 肯定它会为你工作。

You should try doing it like this:你应该尝试这样做:

Response.Redirect("URL", false);
HttpContext.Current.ApplicationInstance.CompleteRequest();

You will be redirected and no error will be thrown.您将被重定向并且不会引发错误。

This is also happening in one of our projects that we migrated from.Net 3.5 to.Net 4.0这也发生在我们从.Net 3.5 迁移到.Net 4.0 的一个项目中

The problem only occurs when compression is enabled .该问题仅在启用压缩时出现。 We had a custom Response Filter in the Global.asax.cs file to apply gzip compression to each response when supported by the browser.我们在Global.asax.cs文件中有一个自定义响应过滤器,以便在浏览器支持时对每个响应应用 gzip 压缩。

The solution was to exclude the addition of that filter when requests were made by AJAX (an update panel).解决方案是在 AJAX(更新面板)发出请求时排除添加该过滤器。 This is easy to distinguish because the request contains a X-MicrosoftAjax: Delta=true header.这很容易区分,因为请求包含X-MicrosoftAjax: Delta=true header。 This can be accomplished by wrapping the logic inside an if:这可以通过将逻辑包装在 if 中来实现:

if (Request.Headers["X-MicrosoftAjax"] != "Delta=true")
{
    // Compression logic here
}

See also: https://forums.asp.net/t/1564697.aspx另见: https://forums.asp.net/t/1564697.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM