Question

Say my current page url has got (http://mysite/english/faq.aspx?faqid=12123&cid=4545&intcid=65456&h=man)

string excludeQuerystring = DynConfig.Item("GoogleSEOLinkSettings/ExcludeQuerystring"); //this is the list of my exclude querystring (cid,intcid,del)

querystring = HttpContext.Current.Request.Url.AbsoluteUri.Split('?')[1]; //I will get faqid=12123&cid=4545,intcid=65456

StringBuilder fullQueryString = new StringBuilder();

if (!string.IsNullOrEmpty(excludeQuerystring) && !string.IsNullOrEmpty(querystring))
{
    string[] strEQ = excludeQuerystring.Split(',');  //making a array of excluded querystrings

    NameValueCollection navValues = HttpUtility.ParseQueryString(querystring);  //getting the list of querystring in NameValueCollection

    if (navValues.Count > 0)
    {
        string[] strQ = navValues.AllKeys;
        if(strQ.Length>0)
        {

        }
    }
}   

querystring= ?+faqid=12123&h=man //here I want updated querystring which does not have any querystring which is there in my excludeQuerystring

I am confused how to get this, actually I want to make a function which will do this all.

Please suggest!!

EDIT:

I applied new code to resolve above problem, however got little stuck while converting NameValueCollection to querystring again.

 protected void Page_Load(object sender, EventArgs e)
        {
            string querystring = string.Empty;

            string excludeList = "cid,intcid,del";

            if (!string.IsNullOrEmpty(excludeList))
            {
                string getFinalString = GetQueryString(excludeList);
                getFinalString = "?" + getFinalString;
            }
        }

        public string GetQueryString(string excludeArray)
        {
            string retQueryString = string.Empty;
            if (excludeArray.IndexOf(",") != -1)
            {
                string[] strArray = excludeArray.Split(",".ToCharArray());
                NameValueCollection filtered = new NameValueCollection();

                filtered.Add(HttpUtility.ParseQueryString(Request.Url.Query));
                if (filtered.HasKeys())
                {
                    foreach (string strMatch in strArray)
                    {
                        filtered.Remove(strMatch);
                    }
                    retQueryString = filtered.ToString(); //Here I am not able to convert back to querystring, however there are other ways to get it like (http://leekelleher.com/2008/06/06/how-to-convert-namevaluecollection-to-a-query-string/), is there any other way to do that
                }
            }

            return retQueryString;
        }
Was it helpful?

Solution

Below is the perfect solution I got it, any comments on this.

string excludeList = "cid,intcid,del";

string getFinalString = Regex.Replace(Regex.Replace(Regex.Replace(Request.Url.Query, @"^\?", "&"), "&(" + excludeList.Replace(",", "|") + ")=[^&]*", "", RegexOptions.IgnoreCase), "^&", "?");

OTHER TIPS

We cannot delete a query string directly like below:

Request.QueryString.Remove("foo")

If you do this, you will get an error - collection is read-only. So, we need to write the below code before deleting the query string.

In C#:

PropertyInfo isreadonly = 
typeof(System.Collections.Specialized.NameValueCollection).GetProperty(
 "IsReadOnly", BindingFlags.Instance | BindingFlags.NonPublic);
 // make collection editable
 isreadonly.SetValue(this.Request.QueryString, false, null);
 // remove
 this.Request.QueryString.Remove("foo");

Hope this will help you !!

yes there is a way to compare two arrays

        var array1 = new byte[] { 1, 2, 5, 4 };
        var array2 = new byte[] { 1, 2, 3, 4 };

        var areEqual = array1.SequenceEqual(array2); //return boolean value True or False
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top