Click here to Skip to main content
12,405,498 members (59,061 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

1.9K views
1 bookmarked
Posted

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

, 15 Dec 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

M.Nadeem Shaikh
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160721.1 | Last Updated 15 Dec 2010
Article Copyright 2010 by M.Nadeem Shaikh
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid