Click here to Skip to main content
15,905,782 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
See more:
Response.Redirect cannot be called in a Page callback error comes sometime when i call following method.Please suggest where i am wrong.
C#
private void GetResult()
 {
     SessionWebsitecl.CheckWebsiteSession();
     m_sTheme = SessionWebsitecl.GetLoggedInWebsite().WebsiteTheme;
     string sUrl = Stringcl.GetValue(Request.ServerVariables[StringConstantcl.HTTP_X_REWRITE_URL]);
     if (Page.IsPostBack)
     {
         if (Request.UrlReferrer != null)
         {
             sUrl = Stringcl.GetValue(Request.UrlReferrer.AbsolutePath);
         }
     }
     if (sUrl.Contains('?'))
     {
         sUrl = sUrl.Substring(0, sUrl.IndexOf('?'));
     }
     HttpModulecl oHttpModulecl = new HttpModulecl();
     string sRedirectedUrl = oHttpModulecl.GetTargetURL(sUrl, sUrl);
     if (sRedirectedUrl != "")
     {
         string sQueryString = Request.Url.Query;
         if (sQueryString != "")
         {
             sRedirectedUrl = sRedirectedUrl + sQueryString;
         }
         Response.Status = "301 Moved Permanently";
         Response.AddHeader("Location", sRedirectedUrl);
         Response.End();
     }
     WebsitePagecl.enmPageType ePageType = WebsitePagecl.enmPageType.DynamicLandingPage;
     WebsitePagecl oWebsitePage = new WebsitePagecl();
     int iPageId = oWebsitePage.GetPageId(sUrl, ePageType);
     if (iPageId != 0)
     {
         if (ePageType != WebsitePagecl.enmPageType.None)
         {
             GeneratePageContent(ePageType, iPageId);
             this.RenderSEODetail(ePageType, iPageId);
         }
     }
     else
     {
         Response.Redirect("/include/information/error_404.aspx");
     }
     string sParameter = Request["__EVENTARGUMENT"]; // this parameter contains url to redirect along with the search prefernce
     string sTarget = Request["__EVENTTARGET"]; // target of the event
     if (sParameter != null && sTarget == "btnSeeMoreLikeThis")
     {
         if (sParameter.Contains("#"))
         {
             int iIndex = sParameter.IndexOf("#");
             sUrl = sParameter.Substring(0, iIndex);
             sParameter = sParameter.Remove(0, iIndex + 1);
             this.SetSearch(sParameter, sUrl);
         }
     }
 }
Posted
Updated 5-Sep-13 23:20pm
v2

1 solution

Hi,
Try to use redirect command as

C#
Response.Redirect("your path",true);

and if still it fires exception then handle it in try,catch.

Hope it helps.
 
Share this answer
 
v2
Comments
k@ran 6-Sep-13 6:06am    
exception does not arise on regular basis.. i dont know why ? Can you please suggest the reason behind that.
Harshil_Raval 6-Sep-13 6:18am    
This exception shows that, redirect command execute before page complete it's call. In simple words, in your function, after your redirect command still some code need to perform. That's why it throw error. To control this behavior, Response.Redirect have two parameters. One is path and second is boolean value. which shows page should abort executing remaining code and redirect or execute code and then redirect to your path.
Kailash Polai 9-Jun-15 2:16am    
When checking for login userid in the session which if expires and then trying to redirect to login page. but at that moment it was callback.

this issue is happening with me also.
k@ran 6-Sep-13 6:31am    
I gone thorough one article where it is mentioned that Response.Redirect("your path",true); through ThreadAbortException.. so again an exception..
Harshil_Raval 6-Sep-13 6:36am    
Yes it will fire exception because by passing true as parameter it force page to redirect to other page. At that time you must handle this redirect command in try,catch. I am not sure but if you pass false as parameter then it will not fire exception, use this if there is no problem if your next code run.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900