65.9K
CodeProject is changing. Read more.
Home

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

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0 vote)

Nov 19, 2010

CPOL
viewsIcon

4950

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.