2011-06-02 7 views
10

यहाँ मेरी कोड है।ASP.NET Response.Redirect() त्रुटि

यहाँ त्रुटि है:

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

किसी को भी मुझे बता सकते हैं क्यों इस त्रुटि हो रहा है?

उत्तर

19

आप बस चल सकता ....

Response.Redirect("~/Membership/UserRegistration.aspx"); 

... आज़माएं/कैच ब्लॉक के बाहर या फिर नीचे दिए John S. Reid's newer solution कोशिश कर सकते हैं:

Response.Redirect(url) ThreadAbortException Solution


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 Microsoft Support article 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:

private bool m_bIsTerminating = false; 

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (WeNeedToRedirect == true) 
    { 
     Response.Redirect(url, false); 
     HttpContext.Current.ApplicationInstance.CompleteRequest(); 
     m_bIsTerminating = true; 

     // Remember to end the method here if there is more code in it. 
     return; 
    } 
} 

protected override void RaisePostBackEvent 
(
    IPostBackEventHandler sourceControl, 
    string eventArgument 
) 
{ 
    if (m_bIsTerminating == false) 
    base.RaisePostBackEvent(sourceControl, eventArgument); 
} 

protected override void Render(HtmlTextWriter writer) 
{ 
    if (m_bIsTerminating == false) 
    base.Render(writer); 
} 

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 - मैंने कोड स्वरूपण को एसओ सीमाओं के अंदर फिट करने के लिए संशोधित किया ताकि यह स्क्रॉल न हो।

+0

अरे, अपने ब्लॉक कोट जो कहता है "* जब आप (यूआरएल) ... * Response.Redirect में एक कॉल कर ThreadAbortException फेंक दिया जाता है", क्या आपको याद है कि आप कहां से है कि मिल गया? मुझे Google के माध्यम से उस उद्धरण के मूल स्रोत को खोजने का प्रयास करने में समस्या हो रही है, क्योंकि वास्तव में इसे कई लोगों द्वारा बहुत चोरी किया गया है:/' –

+0

यह सबसे अच्छा लिंक है जिसके साथ मैं आ सकता हूं, लेकिन यह मूल लेखक का काम नहीं है या तो, यह 31 मार्च, * 2004 * से लेखक होने के नाते एक "जॉन एस रीड" को जोड़ता है और उद्धृत करता है: https://derekreynolds.wordpress.com/2009/10/27/using-response-redirect/। हालांकि मूल लेख अब चला गया प्रतीत होता है। –

+3

द वे बैक मशीन के लिए भगवान का शुक्र है, मुझे * मूल * स्रोत फिर से मिला: [Response.Redirect (url) ThreadAbortException Solution] (https://web.archive.org/web/20120120110234/http://www.c6software .com/लेख/ThreadAbortException.aspx)। –

संबंधित मुद्दे