简体   繁体   English

Response.Redirect在.Net 4.5中不起作用

[英]Response.Redirect doesn't work in .Net 4.5

I am calling response.redirect after verifying the login credentials this all works fine in .net framework 3.5 but when I upgrade to .net 4.5 nothing happens. 我在验证登录凭据后正在调用response.redirect,这在.net Framework 3.5中都可以正常工作,但是当我升级到.net 4.5时,什么都没有发生。 It stays on the same page. 它停留在同一页面上。

I have tried various permutations 我尝试了各种排列

System.Web.HttpContext.Current.Response.Redirect("/");
System.Web.HttpContext.Current.Response.Redirect("/", false);
System.Web.HttpContext.Current.ApplicationInstance.CompleteRequest();

nothing seems to work I have put a try catch around it and I don't get any exceptions when using false. 似乎没有任何工作,我尝试解决了这个问题,使用false时没有任何异常。

The code is being called off of an asp:button onClick event. 正在从asp:button onClick事件中调用代码。 during the click event I check to see if the credentials were correct if they are I perform the login setting session variables ... and redirect them to the appropriate landing page for their role. 在click事件期间,我检查凭据是否正确,是否执行登录设置会话变量...并将其重定向到相应的登陆页面以用作其角色。

More Info I have boiled my problem down to a basic scenario. 更多信息我将问题归结为一个基本情况。 In the following scenario both buttons call the same function btnTemp_Click which calls Response.redirect. 在以下情况下,两个按钮都调用相同的函数btnTemp_Click,该函数调用Response.redirect。 In the case of the asp:button it works in the case of the HTML5 button it doesn't. 对于asp:button,对于HTML5按钮无效。 In both cases the breakpoint on the response.redirect is executed. 在这两种情况下,都会执行response.redirect上的断点。

<asp:Button ID="btnTemp" Text="Test Redirect" runat="server" OnClick="btnTemp_Click" />
<button id="btnTmp2" runat="server" onserverclick="btnTemp_Click" >button to Redirect</button>

protected void btnTemp_Click(object sender, EventArgs e)
{
        HttpContext.Current.Response.Redirect("http://www.google.com", false);
}

Something else that is weird after clicking the button the asp:button no longer works 单击按钮 asp:button后变得奇怪的其他东西不再起作用

If I create a brand new Web application and have nothing in it but these two buttons it works for both. 如果我创建了一个全新的Web应用程序,但是除了这两个按钮之外什么都没有,那么这两个按钮都可以使用。 So I must have some code that is doing something somewhere that the onserverclick hits but the onclick doesn't. 因此,我必须有一些代码正在执行onserverclick所命中但onclick所没有的代码。

solved it using James Johnson's answer here 使用詹姆斯·约翰逊的答案在这里解决

The problem is basically if you are inside an update panel and the control is not specifically registered as a postback trigger then you are trying to redirect an asynchronous postback. 问题基本上是,如果您位于更新面板中,并且该控件未专门注册为回发触发器,那么您将尝试重定向异步回发。

so you have to explicitly register the control as a postback trigger 因此您必须将控件显式注册为回发触发器

James Wrote: 詹姆斯·沃特:

Assuming that you're using an UpdatePanel, add the following ScriptModule to your web.config: 假设您使用的是UpdatePanel,请将以下ScriptModule添加到web.config中:

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

Your other option is to register the control doing the redirect as a postback control. 您的另一个选择是将进行重定向的控件注册为回发控件。 With an UpdatePanel you can do this by adding a PostBackTrigger. 使用UpdatePanel可以通过添加PostBackTrigger来实现。 You can also use the script manager: 您还可以使用脚本管理器:

 ScriptManager.GetCurrent(Page).RegisterPostBackControl(Button1); 

I couldn't get the first solution to work (at least in debug mode) The second solution worked great calling RegisterPostBackControl in the page load. 我无法获得第一个解决方案(至少在调试模式下有效),第二个解决方案在页面加载中调用RegisterPostBackControl非常有效。

Don't know if this is the specific answer to your problem, but it might solve it in a roundabout way, and save somebody lots of time scratching their heads. 不知道这是否是您问题的具体答案,但它可能会以回旋的方式解决,从而节省了很多时间挠头。

I had the issue of a NullReferenceException in an MS Ajax popup - specifically, clicking close on the popup was supposed to go back to the server, and the server-side code redirects to another page, however it was giving this error. 我在MS Ajax弹出窗口中遇到了NullReferenceException问题-具体来说,在弹出窗口上单击关闭应该返回到服务器,并且服务器端代码重定向到另一个页面,但是它给出了此错误。

Looking at the stack trace, I could see that the exception itself was being thrown in the ScriptModule.HttpResponse_Redirecting method. 查看堆栈跟踪,可以看到ScriptModule.HttpResponse_Redirecting方法中引发了异常本身。

Nothing on google would help, and looking in DotPeek didn't really tell me the issue. Google上没有任何帮助,而在DotPeek中浏览并没有真正告诉我这个问题。

Then I saw that it was possible in in VS 2010 upwards to actually load the symbols for the .NET Framework code, so I did this (Tools->Options->Enable .NET Framework Source Stepping). 然后,我发现在VS 2010中可以向上加载.NET Framework代码的符号,所以我做到了(工具->选项->启用.NET Framework源代码步进)。

It turned out that the reason was that the button click was being treated as a callback (rather than a postback), Response.Reirect didn't like this (ie they don't think Response.Redirect should be called when Page.IsCallback is true), and threw an exception internally. 原来原因是按钮单击被视为回调(而不是回发),Response.Reirect不喜欢这样(即,他们不认为应该在Page.IsCallback为时调用Response.Redirect。 true),并在内部引发异常。

Although this was caught (or so it appeared), this was not before it lost its chance to populate internally the RedirectLocation property of the HttpResponse object. 尽管已捕获(或出现了此错误),但这并不是在失去内部填充HttpResponse对象的RedirectLocation属性的机会之前。

The script module code then tried to parse this string, and blew on the fact that it was null. 然后,脚本模块代码尝试解析此字符串,并吹嘘它为null。

The workaround which worked for me therefore was to explicitly set the RedirectLocation property of the HttpResponse object, immediately prior to calling Redirect. 因此,对我有用的解决方法是在调用Redirect之前立即显式设置HttpResponse对象的RedirectLocation属性。 Admittedly it's not a particularly graceful solution, but, let's face it, neither is MS Ajax (nor is WebForms really for that matter! ;) ) 诚然,这不是一个特别优雅的解决方案,但是,让我们面对现实,MS Ajax也不是(WebForms也不是这个真正的问题!;))

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

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