3

I have an issue whereby the company uses a business tracking system that captures a users first visit and redirects them to the page they were trying to visit but with a refferer URL attached... Now as the referer URL carries the url that the person was trying to visit (dont ask why, Ive no idea) it causes me to have a duplicate of every value.

So visiting this...

home.aspx?test=test&test2=test2

becomes

home.aspx?test=test&test2=test2&referrerurl=home.aspx?test=test&test2=test2

Now, URL Encoding the referrer would sort the issue but i've more chance of being molested by a gorilla; so the question stands. How do I dedupe the output from .QueryString ?

Currently I'm using this ...

private string queryClean(string entity)
{
    try
    {
        if(Request.QueryString["referrerUrl"] != null){
        string[] Split = entity.Split(new Char[] { ',' });
        string tmpValue = Convert.ToString(Split[0]);

        return tmpValue;
        }else{ return entity; }
    }
    catch (Exception cleanError) 
    { 
        errors.Text += "-clean-" + cleanError + "-endclean-";
        return entity;
    }

}

(ignore the try/catch we have app level error catching, but I'm still playing with it).

I'm using C# .net 2

Thanks


[Additional Info]

The reffererURL will always be on the end of the string so it could be possible to delete all of the query string after this point?

I've tried using

string test = Convert.ToString(Request.QueryString);
string[] tRSplit = Regex.Split(test, "&referrerUrl");
Response.Write(tRSplit[0]);

and...

System.Collections.Generic.List<string> testb = new System.Collections.Generic.List<string>(Convert.ToString(Request.QueryString).Split('&'));
            for (int i = 0; i < testb.Count; i++)
            {

                Response.Write(i +": "+ testb[i].ToString()+"<br><br>");
            }

Both still produce the duplicates


I could use Trim But is there a cleaner/faster way of achieving this.

Chris McKee
  • 4,298
  • 10
  • 48
  • 83
  • 1
    "i've more chance of being molested by a gorilla" ironically as you say that the advertisement directly beside this post for me involves chimpanzees, they're out for you! – Chris Marisic Feb 03 '09 at 17:43
  • HA! Probably Microsoft Monkeys – Chris McKee Feb 04 '09 at 11:28
  • Just out of curiosity: why/how would URL encoding the referrer solve this issue? Or maybe I don't understand what URL encoding is (I think of replacing space with `%20` and so on...)? – scherand Mar 30 '10 at 07:12
  • url encoded the referer would of been passed like a normal value; as it wasnt .net treated it like 2x the URL parameters. We eventually managed to get it removed after I flailed the idiot who asked for it to be added. – Chris McKee Mar 30 '10 at 08:41

4 Answers4

4

Something like this should work:

NameValueCollection fixedQueryString = new NameValueCollection();
foreach (string key in Request.QueryString.Keys)
{
  string value = Request.QueryString[key];
  if(value.Contains(","))
  {
     value = value.Split(',')[0];
  }
  fixedQueryString.Add(key, value);
}

This code will create a new NameValueCollection where we rebuild the query string. We then check each value for a , which usually indicates that a value is repeated more than once. For instance the url home.aspx?test=test&test2=test2&referrerurl=home.aspx?test=test&test2=test2 will generate the value "test, test" for the query string value "test".

Rune Grimstad
  • 35,612
  • 10
  • 61
  • 76
  • Nice solution but a but the code will be in a class library so I'll have no access to the querystring beyond passing it as a string to the class. – Chris McKee Feb 03 '09 at 12:04
  • You can create a NameValueCollection from the query string itself by using the HttpUtility.ParseQueryString method. Then you might be able to use this method – Rune Grimstad Feb 03 '09 at 12:24
2

I assume you have no control of the redirecting, right? Been there myself, I'm sorry to say. Your problem is theoretically unsolvable, but the tendency seems to be that the lovely people who don't URI encode parameters just append them to the URL, so you could get the raw querystring (from the request.RawUrL property) and just ignore everything after referrerUrl. It's butt-ugly, but basically:

if(Request.QueryString["referrerUrl"] != null)
{
    string qs = request.RawUrl.Substring(request.RawUrl.IndexOf("?") + 1);
    qs = qs.Substring(0, qs.IndexOf("referrerUrl"));
    ReplaceQuerystring(qs);
}

ReplaceQuerystring now receives the querystring as "var1=valA&var2=varB&". Just chop it up on the ampersands, chop every part up on the equals signs, URL decode and store somewhere (actually, request.QueryString should be mutable, so that's probably an option). At least, this is pretty much the way I did it. For your sake, I hope someone comes up with a better solution :)

gustafc
  • 28,465
  • 7
  • 73
  • 99
  • Thanks, and yes your right I can't change it; and even if they agree to change it the release cycle would be to far away. – Chris McKee Feb 03 '09 at 11:09
0

The following example reads all "email" query string parameters and takes only the first occurrence by reading the "email" string and adding the contents to a new string named "email1" until a comma is found. Pretty simple and easy for novices to use.

string email = Request.QueryString["email"];
string email1 = "";
for(int i = 0; i < email.Length; i++)
{
    if (email[i] != ',')
    {
        email1 += email[i].ToString();
    }
    else
    {
        break;
    }
}
email = email1;
user3777549
  • 419
  • 5
  • 8
0

[Example URL]

Default2.aspx?test=test&test2=test2&referrerURL=http://www.test.com/home.aspx?test=test&test=test

[This Would Produce]

test=test
test=test
test2=test2
referrerURL=http://www.test.com/home.aspx?test=test

[So an edited version of gustafc's answer...]

    //Capture the raw url
    test = Request.RawUrl.ToString();
    //Set the start index as 1 after the query ?
    int startTxt = (test.IndexOf("?") + 1);
    //Find the number of characters between ? and &referrerURL
    int countTxt = ((test.IndexOf("&referrerURL")-1) - test.IndexOf("?"));
    //Capture the substring and split into a string array using &
    string[] aTest = test.Substring(startTxt, countTxt).Split(new Char[] { '&' });

[Running a test produces the following]

-0~test=test

-1~test2=test2

[Which is what I expected to recieve]

(for completeness heres the simple for statement)

    for (int i = 0; i < aTest.Length; i++)
    {
        Response.Write("<br>-" + i + "~" + Convert.ToString(aTest[i]) + "<br>");
    }
Chris McKee
  • 4,298
  • 10
  • 48
  • 83