加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – ASP.NET Response.Redirect()错误

发布时间:2020-12-15 18:11:53 所属栏目:百科 来源:网络整理
导读:这是我的代码: try{ Session["CuponeNO"] = txtCode.Text; txtCode.Text = string.Empty; Response.Redirect("~/Membership/UserRegistration.aspx");}catch(Exception ex){ string s = ex.ToString(); lblMessage1.Text = "Error Occured!";} 我收到错误,
这是我的代码:
try
{
    Session["CuponeNO"] = txtCode.Text;
    txtCode.Text = string.Empty;
    Response.Redirect("~/Membership/UserRegistration.aspx");
}
catch(Exception ex)
{
   string s = ex.ToString();
   lblMessage1.Text = "Error Occured!";
}

我收到错误,即使它在catch之后重定向.

这是错误:

“System.Threading.ThreadAbortException:
Thread was being aborted.rn at
System.Threading.Thread.AbortInternal()rn
at
System.Threading.Thread.Abort(Object
stateInfo)rn at
System.Web.HttpResponse.End()rn at
System.Web.HttpResponse.Redirect(String
url,Boolean endResponse)rn at
System.Web.HttpResponse.Redirect(String
url)rn

任何人都可以告诉我为什么会发生这个错误?

解决方法

你可以简单地移动….
Response.Redirect("~/Membership/UserRegistration.aspx");

在Try / Catch块外面,或者你可以尝试下面的John S. Reid’s newer solution:

07001

by John S. Reid
March 31,2004
(edited October 28,2006 to include greater detail and fix some inaccuracies in my analysis,though the solution at it’s core remains the same)

…跳下来…

The ThreadAbortException is thrown when you make a call to Response.Redirect(url) because the system aborts processing of the current web page thread after it sends the redirect to the response stream. Response.Redirect(url) actually makes a call to Response.End() internally,and it’s Response.End() that calls Thread.Abort() which bubbles up the stack to end the thread. Under rare circumstances the call to Response.End() actually doesn’t call Thread.Abort(),but instead calls HttpApplication.CompleteRequest(). (See this 07002 for details and a hint at the solution.)

…跳下来…

PostBack and Render Solutions? Overrides.

The idea is to create a class level variable that flags if the Page should terminate and then check the variable prior to processing your events or rendering your page. This flag should be set after the call to HttpApplication.CompleteRequest(). You can place the check for this value in every PostBack event or rendering block but that can be tedious and prone to errors,so I would recommend just overriding the RaisePostBackEvent and Render methods as in the code sample1 below:

06001

The Final Analysis

Initially I had recommended that you should simply replace all of your calls to Response.Redirect(url) with the Response.Redirect(url,false) and CompleteRequest() calls,but if you want to avoid postback processing and html rendering you’ll need to add the overrides as well. From my recent in depth analysis of the code I can see that the most efficient way to redirect and end processing is to use the Response.Redirect(url) method and let the thread be aborted all the way up the stack,but if this exception is causing you grief as it does in many circumstances then the solution here is the next best thing.

It should also be noted that the Server.Transfer() method suffers from the same issue since it calls Response.End() internally. The good news is that it can be solved in the same way by using the solution above and replacing the call to Response.Redirect() with Server.Execute().

1 – 我修改了代码格式,使其适合在SO边界内,因此它不会滚动.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读