ApplicationInstance.CompleteRequest不会停止其下的code的执行力? [英] ApplicationInstance.CompleteRequest doesn't stop the execution of the code below it?

查看:137
本文介绍了ApplicationInstance.CompleteRequest不会停止其下的code的执行力?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人告诉我,Respond.Redirect是一个昂贵的过程,因为它提出了一个ThreadAbortException。因此,不是,我们应该使用CompleteRequest函数。所以我给它一个尝试,但我注意到codeS下方仍然运行,这是我不想要的。我要立刻强制浏览器跳转到其他网站。

I was told that Respond.Redirect is an expensive process because it raises a ThreadAbortException. So instead, we should be using the CompleteRequest function instead. So I gave it a try but I noticed the codes below it still runs, which I do not want. I want to instantly force the browser to jump to another website.

Public Shared Sub TestCompleteRequest()
            If 1 = 1 Then
                System.Web.HttpContext.Current.Response.Redirect("Http://Google.com", False)
                System.Web.HttpContext.Current.ApplicationInstance.CompleteRequest()
            End If

            Throw New ApplicationException("Hello, why are you here?")
End Sub

对于上述code时,ApplicationException的仍然是抛出。但为什么? (

As for the code above, the ApplicationException is still thrown. But why? :(

推荐答案

一种方法并不直接取代其他。在<一个href=\"http://blogs.msdn.com/b/tmarq/archive/2009/06/25/correct-use-of-system-web-htt$p$psponse-redirect.aspx\"相对=nofollow> CompleteRequest()方法 执行结束时,它的调用。因此,如果这真的是你想要做的Response.Redirect然后(串),这将是一段路要走。

One method doesn't replace the other directly. The CompleteRequest() method does not end execution when it's called. So if that's really what you want to do then Response.Redirect(string) would be the way to go.

CompleteRequest()简单地绕过<一个href=\"http://stackoverflow.com/questions/1087777/is-response-end-considered-harmful\">Response.End()方法,这是产生你提到的ThreadAbortException,但关键CompleteRequest()刷新响应缓冲区。这意味着HTTP 302重定向响应在线路发送到浏览器的 ,你叫CompleteRequest(),它给你一个机会做到这一点不影响反应行动它被送到后用户。

CompleteRequest() simply bypasses the Response.End() method, which is what generates the ThreadAbortException you mentioned, but crucially CompleteRequest() flushes the response buffer. This means the HTTP 302 redirect response is sent to the browser at the line where you call CompleteRequest(), which gives you a chance to do operations that don't affect the response after it's been sent to the user.

您的解决方案实际上取决于你需要实现什么,你可以提供你使用的Response.Redirect什么,什么其他code是在同一个方法的例子吗?

The solution for you really depends on what you need to achieve, can you provide an example of what you're using Response.Redirect for and what other code is in the same method?

这篇关于ApplicationInstance.CompleteRequest不会停止其下的code的执行力?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆