Click here to Skip to main content
Click here to Skip to main content

Remove Request QueryString Key Values for Redirection

By , 23 Jun 2010
 
I wanted to make my page behave in two different modes based on the query string. However it was not strait forward. To make it possible I had to manipulate the URL query string to and remove a key and redirecting it back with a freshly creating URL.
 
Calling Page
 
bool isKeyFound = false; // Vital to stop calling recursively
string newQueryString = AppHelper.GetNewQueryString (Request.QueryString,"refresh", out isKeyFound);
if (isKeyFound && newQueryString.Length > 0)
{
    Response.Redirect(Request.Path + "?" +newQueryString);
}
 
Called Method
 
/// <summary>
/// Recreate a new URL by removing
/// </summary>
/// <param name="queryString">Page Requested string </param>
/// <param name="removeKey">The key to be removed</param>
/// <param name="isKeyFound">This will be a additional info returned</param>
/// <returns></returns>
public static string GetNewQueryString(System.Collections.Specialized.NameValueCollection queryString, string removeKey,out bool isKeyFound)
{
    string returnVal = string.Empty;
    isKeyFound = false ;
    //Build the new query string.
    for (int j = 0; j < queryString.Count; j++)
    {
        // Anything except the target key
        if (!queryString.GetKey(j).ToLower().Equals(removeKey.ToLower()))
        {
            returnVal += queryString.GetKey(j) + "=" + queryString.Get(j) + "&";
        }
        else
        {
            isKeyFound = true;
        }
    }
    //Remove the trailing '&'
    return returnVal.TrimEnd ('&');
}
 

Happy coding

License

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

About the Author

Krish Van Colombo
Web Developer
New Zealand New Zealand
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 23 Jun 2010
Article Copyright 2010 by Krish Van Colombo
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid