Click here to Skip to main content
15,885,782 members
Articles / Web Development / ASP.NET
Alternative
Tip/Trick

Why not to pass endResponse parameter as true in Response.Redirect method

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
15 Dec 2010CPOL 4.9K   1  
Some scenarios to ponder. There is a legacy code which has following implementation Example1 and Example2. If we try to implement MSDN recommendation then the legacy code fails.Here is a Legacy code example:Example 1void Page_Load() { .... some code if(condition) { /// some...

Some scenarios to ponder. There is a legacy code which has following implementation Example1 and Example2. If we try to implement MSDN recommendation then the legacy code fails.


Here is a Legacy code example:


Example 1


void Page_Load() {
  .... some code
  if(condition) {
     /// some condition
  } else {
     RedirectPage(url);
  }
  
  // another code block
  // some other conditions. 
}

Example 2


a. File1.ascx
void Page_Load() {
  try {
    .. some code
    base.CheckPreference();
    RedirectPage(defaultPage);
  }
  catch(Exception ex) {
    ExceptionHandling.GetErrorMessage(ex);
  }
}
b. BaseClass.cs  // this is the base class 
void CheckPreference() {
  try {
     if(condition) {
        RedirectPage(url1);
     } else if(condition2) {
        RedirectPage(url2);
     } else {
        // update session
     }
     
  }
  catch(Exception ex) {
     ExceptionHandling.GetErrorMessage(ex);
     throw;
  }
}
void RedirectPage(string url) {
  Response.Redirect(url);
}

One possible way is to set a class boolean field e.g endExecution, set the field to true whenever RedirectPage is called. We have to update RedirectPage code see code snippet below:


// Updated code - MSDN recommendation.
void RedirectPage(url) {
  Response.Redirect(url, false);
  this.Context.ApplicationInstance.CompleteRequest();
}

Please suggest some other better ways to improve the legacy code implementation.

License

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


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --